TP5.1 傳送簡訊
阿新 • • 發佈:2020-12-02
安裝
composer require phpmailer/phpmailer
直接上程式碼
<?php /** * Created by PhpStorm. * User: Zhangyongfeng * Date: 2020/12/2 * Time: 13:15 * * ━━━━━━━━━神獸出沒━━━━━━━━━ * * ┏┓ ┏┓+ + * ┏┛┻━━━┛┻┓ + + * ┃ ┃ * ┃ ━ ┃ ++ + + + * ████━████ ┃+ * ┃ ┃ + * ┃ ┻ ┃ * ┃ ┃ + + * ┗━┓ ┏━┛ * ┃ ┃ * ┃ ┃ + + + + * ┃ ┃ Code is far away from bug with the animal protecting * ┃ ┃ + 神獸保佑,程式碼無bug * ┃ ┃ * ┃ ┃ + * ┃ ┗━━━┓ + + * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ + + + + * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛+ + + + * * ━━━━━━━━━感覺萌萌噠━━━━━━━━━*/ namespace app\base\controller; use PHPMailer\PHPMailer\PHPMailer; class Mailer extends Base { /* * 傳送郵件 * @param $toemail //收件人地址 * @param $toname //收件人名稱 * @param string $subject //主題 * @param string $body //內容 * @param null $attachment //附件 * @return bool **/ function send_mail($toemail, $toname, $subject = '', $body = '', $attachment = null) { $username = $this->config['cfg_mailUserName']; $password = $this->config['cfg_mailPassword']; $mail = new 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.qq.com"; // 企業郵局域名 $mail->Port = 465; //設定ssl連線smtp伺服器的遠端伺服器埠號 可選465或587 $mail->Username = $username; //郵件傳送人的使用者名稱(請填寫完整的email地址) $mail->Password = $password; // 郵件傳送人的 密碼 (授權碼) $mail->SetFrom($username, $username); $replyEmail = ''; //留空則為發件人EMAIL $replyName = ''; //回覆名稱(留空則為發件人名稱) $mail->AddReplyTo($replyEmail, $replyName); //回覆的地址 $mail->Subject = $subject; //郵件標題 $mail->MsgHTML($body); //郵件內容 $mail->AddAddress($toemail, $toname); //收件人地址,("收件人email","收件人姓名") if (is_array($attachment)) { // 新增附件 foreach ($attachment as $file) { is_file($file) && $mail->AddAttachment($file); } } return $mail->Send() ? true : $mail->ErrorInfo; } }
呼叫
// 郵箱傳送 public function mailer() { $toemail = '[email protected]'; $toname = '1322816443'; $subject = 'QQ郵件傳送測試'; $content = '<h1>恭喜你,郵件測試成功。</h1>'; dump($this->mailer->send_mail($toemail, $toname, $subject, $content)); }