thinkphp5中如何正確使用phpmailer發送郵件
阿新 • • 發佈:2019-02-27
php end password iso ext think 協議 errors con
步驟1、先下載phpmailer放到vendor目錄,如下圖
步驟2、在公共函數文件common.php中加入以下代碼:
function send_email($to,$subject=‘‘,$content=‘‘){ vendor(‘phpmailer.PHPMailerAutoload‘); ////require_once vendor/phpmailer/PHPMailerAutoload.php‘; $mail = new PHPMailer; $mail->CharSet = ‘UTF-8‘; //設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置,否則亂碼 $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 0; //調試輸出格式 //$mail->Debugoutput = ‘html‘; //smtp服務器 $mail->Host = config(‘email.smtp_server‘); //端口 - likely to be 25, 465 or 587 $mail->Port = 465; if($mail->Port === 465) $mail->SMTPSecure = ‘ssl‘;// 使用安全協議 //Whether to use SMTP authentication $mail->SMTPAuth = true; //發送郵箱 $mail->Username = config(‘email.smtp_user‘); //密碼 $mail->Password = config(‘email.smtp_pwd‘); //Set who the message is to be sent from $mail->setFrom(config(‘email.smtp_user‘),‘尊敬的用戶‘); //回復地址 //$mail->addReplyTo(‘[email protected]‘, ‘First Last‘); //接收郵件方 if(is_array($to)){ foreach ($to as $v){ $mail->addAddress($v); } }else{ $mail->addAddress($to); } $mail->isHTML(true);// send as HTML //標題 $mail->Subject = $subject; //HTML內容轉換 $mail->msgHTML($content); //Replace the plain text body with one created manually //$mail->AltBody = ‘This is a plain-text message body‘; //添加附件 //$mail->addAttachment(‘images/phpmailer_mini.png‘); //send the message, check for errors return $mail->Send(); }
步驟3、在config.php中增加以下代碼:
//email ‘email‘ => [ ‘smtp_server‘ => ‘郵箱服務地址‘, ‘smtp_port‘ => ‘端口號‘, ‘smtp_user‘ => ‘用戶名‘, ‘smtp_pwd‘ => ‘密碼‘, ],
步驟4、調用
send_email($to, $title, $content); //$to 要發送的地址 //$title 郵件標題 //$content 郵件內容
thinkphp5中如何正確使用phpmailer發送郵件