郵件配置
'THINK_EMAIL' => array(
'SMTP_HOST' => 'smtp.163.com', //SMTP服務器
'SMTP_PORT' => '465', //SMTP服務器端口
'SMTP_USER' => 'ihtxt@163.com', //SMTP服務器用戶名
'SMTP_PASS' => 'ihtxt.com', //SMTP服務器密碼
'FROM_EMAIL' => 'ihtxt@163.com', //發件人EMAIL
'FROM_NAME' => '愛紅電子書', //發件人名稱
'REPLY_EMAIL' => 'ihtxt@163.com', //回覆EMAIL(留空則爲發件人EMAIL)
'REPLY_NAME' => '愛紅電子書', //回覆名稱(留空則爲發件人名稱)
),
注:推薦使用163郵件,不要使用QQ郵箱,郵件配置必定要正確
能夠將該函數放到ThinkPHPCommoncommon.php。也能夠放到你生成的項目文件的Commoncommon.php。不過只能放到一個地方,不然就會報函數重定義錯誤。
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->AltBody = "爲了查看該郵件,請切換到支持 HTML 的郵件客戶端";
$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;
}
$r = think_send_mail('要發送的郵箱','發送人名稱,即你的名稱','文件標題','郵件內容');
windows下在php.ini中去掉下面的分號
extension=php_openssl.dll
並將allow_url_fopen = Off改成
allow_url_fopen = On