使用外部依賴phpmailer,通過github下載(PHPMailer.php,Exception.php,SMTP.php)
PHPMailer.php修改中文編碼:
public $CharSet = self::CHARSET_ISO88591;
修改為:
public $CharSet = self::CHARSET_UTF8;
發(fā)信示例代碼:
<?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'; //企業(yè)郵箱服務(wù)器 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; //授權(quán)
$mail->Username = 'a@example.cn'; //發(fā)信地址
$mail->Password = '**********'; //未啟用授權(quán)碼填寫發(fā)信郵箱密碼;已啟用授權(quán)碼則填寫客戶端授權(quán)碼
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //啟用加密
//Recipients
$mail->setFrom('a@example.cn', 'Mailer'); //顯示發(fā)信地址
$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 = '測試 內(nèi)容';
//$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}";
}
?>