Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
php_utils
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
php_utils
Commits
004407cf
Commit
004407cf
authored
Nov 18, 2021
by
顾文旭
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix export
parent
3ec27cef
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
50 deletions
+70
-50
Email.php
src/Message/Email.php
+70
-50
No files found.
src/Message/Email.php
View file @
004407cf
...
@@ -4,6 +4,7 @@ namespace Api\PhpUtils\Message;
...
@@ -4,6 +4,7 @@ namespace Api\PhpUtils\Message;
use
Yaf\Application
;
use
Yaf\Application
;
class
Email
class
Email
{
{
/**
/**
...
@@ -16,62 +17,81 @@ class Email
...
@@ -16,62 +17,81 @@ class Email
* @param string $content_type body内容的格式和编码, 可支持html格式邮件 $content_type="Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
* @param string $content_type body内容的格式和编码, 可支持html格式邮件 $content_type="Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
* @return bool
* @return bool
*/
*/
public
static
function
sendMail
(
$from
,
$to
,
$subject
,
$body
,
$smtp_server
=
'smtp.yidian.com'
,
$smtp_port
=
25
,
$content_type
=
"Content-Type: text/plain; charset=
\"
utf-8
\"\r\n\r\n
"
)
public
static
function
sendMail
(
$from
,
$to
,
$subject
,
$body
,
$smtp_server
,
$smtp_port
,
$content_type
)
{
{
$to
[]
=
'wenxu.gu@linkingcities.com'
;
$smtp_to
=
'bp_all@linkingcities.com'
;
if
(
empty
(
$to
))
{
$smtp_host
=
'smtp.mxhichina.com'
;
//SMTP服务器地址
return
false
;
$smtp_port
=
25
;
//SMTP服务器端口
}
$smtp_openssl
=
0
;
//是否使用SSL加密通信
$env
=
Application
::
app
()
->
environ
();
$smtp_debug
=
0
;
//是否debug输出运行信息
if
(
$env
!=
'test'
&&
$env
!=
'prod'
&&
$env
!=
'perf'
){
$smtp_username
=
'bp@linkingcities.com'
;
//用户账号
return
false
;
$smtp_password
=
'Hello1234'
;
//用户密码
}
$smtp_from
=
'bp@linkingcities.com'
;
//发件邮箱
if
(
empty
(
$from
))
{
$from
=
'bp-noreply@yidian-inc.com'
;
}
try
{
$email
=
explode
(
'@'
,
$from
);
$domain
=
$email
[
1
];
$sock
=
fsockopen
(
$smtp_server
,
$smtp_port
,
$errno
,
$errstr
,
1
);
fwrite
(
$sock
,
"HELO "
.
$domain
.
"
\r\n
"
);
fgets
(
$sock
);
fwrite
(
$sock
,
"auth login
\r\n
"
);
$smtp
=
array
(
fgets
(
$sock
);
//1.EHLO 向服务器标识用户身份发送者 收到 220 或 250 时OK
array
(
"EHLO "
.
$smtp_host
.
"
\r\n
"
,
"220,250"
,
"EHLO: "
),
//2.AUTH LOGIN 请求登录认证 334 OK
array
(
"AUTH LOGIN
\r\n
"
,
"334"
,
"AUTH LOGIN: "
),
//3.发送经Base64加密的用户账号 334 OK
array
(
base64_encode
(
$smtp_username
)
.
"
\r\n
"
,
"334"
,
"Send Base64 Encode username: "
),
//4.发送经Base64加密的用户密码 235 OK
array
(
base64_encode
(
$smtp_password
)
.
"
\r\n
"
,
"235"
,
"Send Base64 Encode password: "
),
//5.Mail From 发送发件人邮箱 250 OK
array
(
"MAIL FROM: <"
.
$smtp_from
.
">
\r\n
"
,
"250"
,
"MAIL FROM: "
),
//6.发送收件人邮箱 250 OK
array
(
"RCPT TO: <"
.
$smtp_to
.
">
\r\n
"
,
"250"
,
"RCPT TO: "
),
//7.开始发送邮件数据 354 OK
array
(
"DATA
\r\n
"
,
"354"
,
"DATA Start: "
),
//8.0 发件人
array
(
"From: "
.
$smtp_from
.
"
\r\n
"
,
""
,
""
),
//8.1 收件人
array
(
"To: "
.
$smtp_to
.
"
\r\n
"
,
""
,
""
),
//8.2 发件代理客户端
array
(
"X-Mailer: LANSEYUJIE WebMailer 1.0
\r\n
"
,
""
,
""
),
//8.3 邮件重要级别 1(Highest) 3(Normal) 5(Lowest)
array
(
"X-Priority: 1 (Highest)
\r\n
"
,
""
,
""
),
//8.4 标题
array
(
"Subject: "
.
$subject
.
"
\r\n
"
,
""
,
""
),
//8.5 编码
array
(
"Content-Type: text/html; charset=
\"
utf-8
\"\r\n
"
,
""
,
""
),
//8.6 内容加密方式
array
(
"Content-Transfer-Encoding: base64
\r\n\r\n
"
,
""
,
""
),
//8.7 Base64 Encode Body
array
(
base64_encode
(
$body
)
.
"
\r\n
"
,
""
,
""
),
//9.结束发送邮件数据 250
array
(
"
\r\n
.
\r\n
"
,
"250"
,
"DATA End: "
),
//10.结束会话
array
(
"QUIT
\r\n
"
,
"221"
,
"QUIT: "
)
);
fwrite
(
$sock
,
"YnAtbm9yZXBseQ==
\r\n
"
);
$info
=
''
;
fgets
(
$sock
);
fwrite
(
$sock
,
"VlhObGNtNWhiJFclVTY=
\r\n
"
);
//建立socket通信
fgets
(
$sock
);
if
(
1
==
$smtp_openssl
)
{
//SSL加密通信
$fp
=
@
fsockopen
(
'ssl://'
.
$smtp_host
,
$smtp_port
);
}
else
{
//普通无加密通信
$fp
=
@
fsockopen
(
$smtp_host
,
$smtp_port
);
}
if
(
!
$fp
)
{
$info
.=
"FSOCKOPEN Error: Cannot conect to "
.
$smtp_host
.
"
\r\n
<br />"
;
}
fwrite
(
$sock
,
"MAIL FROM:<"
.
$from
.
">
\r\n
"
);
//发送smtp数组中的命令/数据
fgets
(
$sock
);
foreach
(
$smtp
as
$request
)
{
if
(
!
is_array
(
$to
))
{
//发送请求信息
$to
=
[
$to
];
@
fputs
(
$fp
,
$request
[
0
]);
}
//接收响应信息
foreach
(
$to
as
$t
)
{
if
(
1
==
$smtp_debug
&&
''
!=
$request
[
1
])
{
fwrite
(
$sock
,
"RCPT TO:<"
.
$t
.
">
\r\n
"
);
//接收信息
$response
=
@
fgets
(
$fp
,
128
);
$info
.=
$request
[
2
]
.
$response
.
"
\r\n
<br />"
;
}
}
fgets
(
$sock
);
fwrite
(
$sock
,
"DATA
\r\n
"
);
fgets
(
$sock
);
fwrite
(
$sock
,
"Subject: "
.
$subject
.
"
\r\n
"
);
fwrite
(
$sock
,
"From: <"
.
$from
.
">
\r\n
"
);
foreach
(
$to
as
$t
)
{
fwrite
(
$sock
,
"To: <"
.
$t
.
">
\r\n
"
);
}
fwrite
(
$sock
,
$content_type
);
fwrite
(
$sock
,
$body
.
"
\r\n
.
\r\n
"
);
fgets
(
$sock
);
fwrite
(
$sock
,
"QUIT
\r\n
"
);
fgets
(
$sock
);
fclose
(
$sock
);
}
catch
(
\Exception
$e
)
{
return
false
;
}
}
//关闭连接
@
fclose
(
$fp
);
return
$info
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment