tp5發送email功能: PHPmailer

來源:互聯網
上載者:User

標籤:名稱   content   body   tac   from   發送郵件   pass   str   target   

第一步:使用composer安裝phpmailer

  1. 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.