在本身項目引入核心類庫文件html
require_once "class.phpmailer.php"; require_once "class.smtp.php";
我用的框架是thinkphp,因此我把類庫文件重命名
class.phpmailer.php
重命名爲PHPMailer.class.php
。
class.smtp.php
重命名爲SMTP.class.php
。ajax
// smtp服務器配置 'SMTP_SERVER' =>array( 'CharSet' => 'UTF-8', //編碼格式 'SMTPSecure' => 'ssl', //郵件協議 'Port' => 465, //smtp服務器端口 'Host' => '', //smtp服務器地址 'Username' => '', //郵箱登陸帳戶 'Password' => '', //smtp受權碼 'From' => '', //郵件發送郵箱 'FromName' => '', //發件人名稱 ),
use Org\Mailer\PHPMailer; use Org\Mailer\SMTP; //在控制器中使用的時候先讀取SMTP配置(thinkphp用C函數讀取配置) $smtpConfig = C( 'SMTP_SERVER' ); $smtpConfig = C( 'SMTP_SERVER' ); // 發送郵件 try { $mail = new PHPMailer( true ); $mail->IsSMTP(); $mail->CharSet = $smtpConfig[ 'CharSet' ]; //郵件字符編碼 $mail->SMTPSecure = $smtpConfig[ 'SMTPSecure' ]; //郵件傳輸協議 $mail->Port = $smtpConfig[ 'Port' ]; //SMTP服務器端口 $mail->Host = $smtpConfig[ 'Host' ]; //SMTP服務器地址 $mail->Username = $smtpConfig[ 'Username' ]; //郵箱登陸賬號 $mail->Password = $smtpConfig[ 'Password' ]; //SMTP受權碼 //$mail->AddReplyTo("phpddt1990@163.com","mckee"); //回覆地址 $mail->From = $smtpConfig[ 'From' ]; //郵件發送人郵箱 $mail->FromName = $smtpConfig[ 'FromName' ]; //郵件發送人暱稱 $mail->SMTPAuth = true; //開啓認證 //$mail->SMTPDEBUG = 0; //關閉debug $to = ""; //郵件收件人郵箱 $mail->AddAddress( $to ); $mail->Subject = ""; //郵件標題 $mail->Body = "" //郵件正文 // $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //當郵件不支持html時備用顯示,能夠省略 $mail->WordWrap = 200; // 設置每行字符串的長度 //$mail->AddAttachment("f:/test.png"); //添加附件 $mail->IsHTML( true ); $mail->Send(); $this->ajaxReturn( 'mailVerify' ); } catch ( phpmailerException $e ) { $this->ajaxReturn( false ); }