使用外部依赖phpmailer,通过github下载(PHPMailer.php,Exception.php,SMTP.php)
PHPMailer.php修改中文编码:
public $CharSet = self::CHARSET_ISO88591;
修改为:
public $CharSet = self::CHARSET_UTF8;
发信示例代码:
<?php
require 'PHPMailer.php';
require 'Exception.php';
require 'SMTP.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->Charset='UTF-8';
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'ssl://smtp-n.global-mail.cn'; //企业邮箱服务器 smtp-n.global-mail.cn 或 smtp.global-mail.cn
$mail->Port = 465; //端口
//$mail->Host = 'smtp-n.global-mail.cn';
//$mail->Port = 25;
$mail->SMTPAuth = true; //授权
$mail->Username = 'a@example.cn'; //发信地址
$mail->Password = '**********'; //未启用授权码填写发信邮箱密码;已启用授权码则填写客户端授权码
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //启用加密
//Recipients
$mail->setFrom('a@example.cn', 'Mailer'); //显示发信地址
$mail->addAddress('test1@example.cn','a@example.cn'); //收件人和昵称
//$mail->addAddress('test1@example.cn'); //昵称也可不填
$mail->addReplyTo('a@example.cn', 'Information');//回信地址
$mail->addCC('test2@example.cn');//抄送人
$mail->addBCC('test3@example.cn');//密送人
//Attachments
//$mail->addAttachment('C:/Users/Documents/test.jpg'); //增加附件
//$mail->addAttachment('C:/Users/Documents/test.jpg', 'new.jpg'); //名称可选
//Content
$mail->isHTML(true); //HTML 格式
$mail->Subject = '测试 标题';
$mail->Body = '测试 内容';
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->Sender = 'a@example.cn';
echo 'Message has been sent';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>