最近在做一个项目,需要有邮件发送的功能,服务器邮件发送的话,服务器上必须有能链接邮件服务器,才能实现以下的步骤,现在就给大家分享一下,专门做了一个邮件的发送类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/**
* 系统邮件发送函数
* @param string $to 接收邮件者邮箱
* @param string $name 接收邮件者名称
* @param string $subject 邮件主题
* @param string $body 邮件内容
* @param string $attachment 附件列表
* @return boolean
*/
function
think_send_mail(
$to
,
$name
,
$subject
=
''
,
$body
=
''
,
$attachment
= null){
$config
= C(
'THINK_EMAIL'
);
vendor(
'PHPMailer.class#phpmailer'
);
//从PHPMailer目录导class.phpmailer.php类文件
$mail
=
new
PHPMailer();
//PHPMailer对象
$mail
->CharSet =
'UTF-8'
;
//设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
$mail
->IsSMTP();
// 设定使用SMTP服务
$mail
->SMTPDebug = 0;
// 关闭SMTP调试功能
// 1 = errors and messages
// 2 = messages only
$mail
->SMTPAuth = true;
// 启用 SMTP 验证功能
$mail
->SMTPSecure =
'ssl'
;
// 使用安全协议
$mail
->Host =
$config
[
'SMTP_HOST'
];
// SMTP 服务器
$mail
->Port =
$config
[
'SMTP_PORT'
];
// SMTP服务器的端口号
$mail
->Username =
$config
[
'SMTP_USER'
];
// SMTP服务器用户名
$mail
->Password =
$config
[
'SMTP_PASS'
];
// SMTP服务器密码
$mail
->SetFrom(
$config
[
'FROM_EMAIL'
],
$config
[
'FROM_NAME'
]);
$replyEmail
=
$config
[
'REPLY_EMAIL'
]?
$config
[
'REPLY_EMAIL'
]:
$config
[
'FROM_EMAIL'
];
$replyName
=
$config
[
'REPLY_NAME'
]?
$config
[
'REPLY_NAME'
]:
$config
[
'FROM_NAME'
];
$mail
->AddReplyTo(
$replyEmail
,
$replyName
);
$mail
->Subject =
$subject
;
$mail
->MsgHTML(
$body
);
$mail
->AddAddress(
$to
,
$name
);
if
(
is_array
(
$attachment
)){
// 添加附件
foreach
(
$attachment
as
$file
){
is_file
(
$file
) &&
$mail
->AddAttachment(
$file
);
}
}
return
$mail
->Send() ? true :
$mail
->ErrorInfo;
}
|
此函数只能在ThinkPHP中使用且需要phpmailer扩展的支持;
phpmailer扩展的放置目录为 ThinkPHP/Extend/Vendor/PHPMailer/class.phpmailer.php
phpmail的下载地址:
https://code.google.com/a/apache-extras.org/p/phpmailer
使用此函数 必须在项目中加入以下配置项
1
2
3
4
5
6
7
8
9
10
11
|
//邮件配置
'THINK_EMAIL'
=>
array
(
'SMTP_HOST'
=>
'smtp.aaa.com'
,
//SMTP服务器
'SMTP_PORT'
=>
'465'
,
//SMTP服务器端口
'SMTP_USER'
=>
'mail@aaa.com'
,
//SMTP服务器用户名
'SMTP_PASS'
=>
'password'
,
//SMTP服务器密码
'FROM_EMAIL'
=>
'mail@aaa.com'
,
//发件人EMAIL
'FROM_NAME'
=>
'ThinkPHP'
,
//发件人名称
'REPLY_EMAIL'
=>
''
,
//回复EMAIL(留空则为发件人EMAIL)
'REPLY_NAME'
=>
''
,
//回复名称(留空则为发件人名称)
),
|
本文转自 3147972 51CTO博客,原文链接:http://blog.51cto.com/a3147972/1221287,如需转载请自行联系原作者