標籤:名稱 content body tac from 發送郵件 pass str target
第一步:使用composer安裝phpmailer
- composer require phpmailer/phpmailer
第二步:common.php寫個發送郵件的函數
/** * 系統郵件發送函數 * @param string $tomail 內送郵件者郵箱 * @param string $name 內送郵件者名稱 * @param string $subject 郵件主題 * @param string $body 郵件內容 * @param string $attachment 附件列表 * @return boolean * @author static7 <[email protected]> */function send_mail($tomail, $name, $subject = ‘‘, $body = ‘‘, $attachment = null) { $mail = new \phpmailar\PHPMailer(); //執行個體化PHPMailer對象 $mail->CharSet = ‘UTF-8‘; //設定郵件編碼,預設ISO-8859-1,如果發中文此項必須設定,否則亂碼 $mail->IsSMTP(); // 設定使用SMTP服務 $mail->SMTPDebug = 0; // SMTP調試功能 0=關閉 1 = 錯誤和訊息 2 = 訊息 $mail->SMTPAuth = true; // 啟用 SMTP 驗證功能 $mail->SMTPSecure = ‘ssl‘; // 使用安全性通訊協定 $mail->Host = "smtp.163.com"; // SMTP 伺服器 $mail->Port = 465; // SMTP伺服器的連接埠號碼 $mail->Username = "[email protected]"; // SMTP伺服器使用者名稱 $mail->Password = ""; // SMTP伺服器密碼 $mail->SetFrom(‘[email protected]‘, ‘xxx‘); $replyEmail = ‘‘; //留空則為寄件者EMAIL $replyName = ‘‘; //回複名稱(留空則為寄件者名稱) $mail->AddReplyTo($replyEmail, $replyName); $mail->Subject = $subject; $mail->MsgHTML($body); $mail->AddAddress($tomail, $name); if (is_array($attachment)) { // 添加附件 foreach ($attachment as $file) { is_file($file) && $mail->AddAttachment($file); } } return $mail->Send() ? true : $mail->ErrorInfo;}
注意以上代碼,由於composer安裝的PHPmailer類在phpmailer包中因此執行個體化時正確路由是 new \phpmailer\PHPmailer()
第三步:控制器方法裡寫發送的內容
public function email() { $toemail=‘[email protected]‘; $name=‘static7‘; $subject=‘QQ郵件發送測試‘; $content=‘恭喜你,郵件測試成功。‘; send_mail($toemail,$name,$subject,$content); }
第四步:測試發送
部落格連結:https://www.calm7.com/article/5.html
tp5發送email功能: PHPmailer