切换导航条
此项目
正在载入...
登录
何书鹏
/
recruit
·
提交
转到一个项目
GitLab
转到仪表盘
项目
活动
文件
提交
管道
0
构建
0
图表
里程碑
问题
0
合并请求
0
成员
标记
维基
派生
网络
创建新的问题
下载为
邮件补丁
差异文件
浏览文件
作者
Karson
5 years ago
提交
290bfc4f7544b0f99b717fb9d8d445bfcba73e87
1 个父辈
30c695ff
新增邮件抄送、附件方法
更新PHPMailer到6.0版本
隐藏空白字符变更
内嵌
并排对比
正在显示
2 个修改的文件
包含
98 行增加
和
53 行删除
application/common/library/Email.php
composer.json
application/common/library/Email.php
查看文件 @
290bfc4
...
...
@@ -27,7 +27,7 @@ class Email
*/
public
$options
=
[
'charset'
=>
'utf-8'
,
//编码格式
'debug'
=>
0
,
//调式模式
'debug'
=>
false
,
//调式模式
];
/**
...
...
@@ -55,14 +55,17 @@ class Email
$this
->
options
=
array_merge
(
$this
->
options
,
$config
);
}
$this
->
options
=
array_merge
(
$this
->
options
,
$options
);
vendor
(
'phpmailer.phpmailer.PHPMailerAutoload'
);
$securArr
=
[
1
=>
'tls'
,
2
=>
'ssl'
];
$this
->
mail
=
new
\PHPMailer
(
true
);
$this
->
mail
=
new
\PHPMailer
\PHPMailer\PHPMailer
(
true
);
$this
->
mail
->
CharSet
=
$this
->
options
[
'charset'
];
$this
->
mail
->
SMTPDebug
=
$this
->
options
[
'debug'
];
$this
->
mail
->
isSMTP
();
$this
->
mail
->
SMTPAuth
=
true
;
if
(
$this
->
options
[
'mail_type'
]
==
1
)
{
$this
->
mail
->
SMTPDebug
=
$this
->
options
[
'debug'
];
$this
->
mail
->
isSMTP
();
$this
->
mail
->
SMTPAuth
=
true
;
}
else
{
$this
->
mail
->
isMail
();
}
$this
->
mail
->
Host
=
$this
->
options
[
'mail_smtp_host'
];
$this
->
mail
->
Username
=
$this
->
options
[
'mail_from'
];
$this
->
mail
->
Password
=
$this
->
options
[
'mail_smtp_pass'
];
...
...
@@ -75,55 +78,118 @@ class Email
/**
* 设置邮件主题
* @param string $subject
* @param string $subject
邮件主题
* @return $this
*/
public
function
subject
(
$subject
)
{
$this
->
options
[
'subject'
]
=
$subject
;
$this
->
mail
->
Subject
=
$subject
;
return
$this
;
}
/**
* 设置发件人
* @param string $email
* @param string $name
* @param string $email 发件人邮箱
* @param string $name 发件人名称
* @return $this
*/
public
function
from
(
$email
,
$name
=
''
)
{
$this
->
options
[
'from'
]
=
$email
;
$this
->
options
[
'from_name'
]
=
$name
;
$this
->
mail
->
setFrom
(
$email
,
$name
);
return
$this
;
}
/**
* 设置收件人
* @param string $email
* @param string $name
* @param mixed $email 收件人,多个收件人以,进行分隔
* @param string $name 收件人名称
* @return $this
*/
public
function
to
(
$email
,
$name
=
''
)
{
$this
->
options
[
'to'
]
=
$email
;
$this
->
options
[
'to_name'
]
=
$name
;
$emailArr
=
$this
->
buildAddress
(
$email
);
foreach
(
$emailArr
as
$address
=>
$name
)
{
$this
->
mail
->
addAddress
(
$address
,
$name
);
}
return
$this
;
}
/**
* 设置抄送
* @param mixed $email 收件人,多个收件人以,进行分隔
* @param string $name 收件人名称
* @return Email
*/
public
function
cc
(
$email
,
$name
=
''
)
{
$emailArr
=
$this
->
buildAddress
(
$email
);
foreach
(
$emailArr
as
$address
=>
$name
)
{
$this
->
mail
->
addCC
(
$address
,
$name
);
}
return
$this
;
}
/**
* 设置密送
* @param mixed $email 收件人,多个收件人以,进行分隔
* @param string $name 收件人名称
* @return Email
*/
public
function
bcc
(
$email
,
$name
=
''
)
{
$emailArr
=
$this
->
buildAddress
(
$email
);
foreach
(
$emailArr
as
$address
=>
$name
)
{
$this
->
mail
->
addBCC
(
$address
,
$name
);
}
return
$this
;
}
/**
* 设置邮件正文
* @param string $body
* @param boolean $ishtml
* @param string $body 邮件下方
* @param boolean $ishtml 是否HTML格式
* @return $this
*/
public
function
message
(
$body
,
$ishtml
=
true
)
{
$this
->
options
[
'body'
]
=
$body
;
$this
->
options
[
'ishtml'
]
=
$ishtml
;
if
(
$ishtml
)
{
$this
->
mail
->
msgHTML
(
$body
);
}
else
{
$this
->
mail
->
Body
=
$body
;
}
return
$this
;
}
/**
* 添加附件
* @param string $path 附件路径
* @param string $name 附件名称
* @return Email
*/
public
function
attachment
(
$path
,
$name
=
''
)
{
$this
->
mail
->
addAttachment
(
$path
,
$name
);
return
$this
;
}
/**
* 构建Email地址
* @param mixed $emails Email数据
* @return array
*/
protected
function
buildAddress
(
$emails
)
{
$emails
=
is_array
(
$emails
)
?
$emails
:
explode
(
','
,
str_replace
(
";"
,
","
,
$emails
));
$result
=
[];
foreach
(
$emails
as
$key
=>
$value
)
{
$email
=
is_numeric
(
$key
)
?
$value
:
$key
;
$result
[
$email
]
=
is_numeric
(
$key
)
?
""
:
$value
;
}
return
$result
;
}
/**
* 获取最后产生的错误
* @return string
*/
...
...
@@ -148,38 +214,17 @@ class Email
public
function
send
()
{
$result
=
false
;
switch
(
$this
->
options
[
'mail_type'
])
{
case
1
:
//使用phpmailer发送
$this
->
mail
->
setFrom
(
$this
->
options
[
'from'
],
$this
->
options
[
'from_name'
]);
$this
->
mail
->
addAddress
(
$this
->
options
[
'to'
],
$this
->
options
[
'to_name'
]);
$this
->
mail
->
Subject
=
$this
->
options
[
'subject'
];
if
(
$this
->
options
[
'ishtml'
])
{
$this
->
mail
->
msgHTML
(
$this
->
options
[
'body'
]);
}
else
{
$this
->
mail
->
Body
=
$this
->
options
[
'body'
];
}
try
{
$result
=
$this
->
mail
->
send
();
}
catch
(
\phpmailerException
$e
)
{
$this
->
setError
(
$e
->
getMessage
());
}
$this
->
setError
(
$result
?
''
:
$this
->
mail
->
ErrorInfo
);
break
;
case
2
:
//使用mail方法发送邮件
$headers
=
'MIME-Version: 1.0'
.
"
\r\n
"
;
$headers
.=
"Content-type: text/html; charset="
.
$this
->
options
[
'charset'
]
.
"
\r\n
"
;
$headers
.=
"To:
{
$this
->
options
[
'to_name'
]}
<
{
$this
->
options
[
'to'
]}
>
\r\n
"; //收件人
$headers
.= "
From
:
{
$this
->
options
[
'from_name'
]}
<
{
$this
->
options
[
'from'
]}
>
\r\n
"; //发件人
$result
= mail(
$this->options
['to'],
$this->options
['subject'],
$this->options
['body'],
$headers
);
$this->setError
(
$result
? '' : error_get_last()['message']);
break;
default:
//邮件功能已关闭
$this->setError
(__('Mail already closed'));
break;
if
(
in_array
(
$this
->
options
[
'mail_type'
],
[
1
,
2
]))
{
try
{
$result
=
$this
->
mail
->
send
();
}
catch
(
\PHPMailer\PHPMailer\Exception
$e
)
{
$this
->
setError
(
$e
->
getMessage
());
}
$this
->
setError
(
$result
?
''
:
$this
->
mail
->
ErrorInfo
);
}
else
{
//邮件功能已关闭
$this
->
setError
(
__
(
'Mail already closed'
));
}
return
$result
;
}
...
...
composer.json
查看文件 @
290bfc4
...
...
@@ -21,7 +21,7 @@
"endroid/qr-code"
:
"^1.9"
,
"topthink/think-captcha"
:
"^1.0"
,
"mtdowling/cron-expression"
:
"^1.2"
,
"phpmailer/phpmailer"
:
"
^5.2
"
,
"phpmailer/phpmailer"
:
"
~6.0.6
"
,
"karsonzhang/fastadmin-addons"
:
"~1.1.9"
,
"overtrue/pinyin"
:
"~3.0"
,
"phpoffice/phpspreadsheet"
:
"^1.2"
...
...
请
注册
或
登录
后发表评论