thinkPHP發(fā)送郵件
歡迎大家訪問我的主頁:西城Gether學習網
我的博客:淺作序
1. 使用composer安裝phpmailer組件
進入Packmailer官網,搜索phpmailer組件。
-
在項目目錄下使用命令
composer require phpmailer/phpmailer
安裝組件。- 安裝成功后,你就可以找到vendor/phpmailer文件夾。
-
在 Packmailer官網的phpmailer 頁面找到:
A Simple Example
<?php // Import PHPMailer classes into the global namespace // These must be at the top of your script, not inside a function use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; //Load Composer's autoloader require 'vendor/autoload.php'; $mail = new PHPMailer(true); // Passing `true` enables exceptions try { //Server settings $mail->SMTPDebug = 2; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'user@example.com'; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to //Recipients $mail->setFrom('from@example.com', 'Mailer'); $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient $mail->addAddress('ellen@example.com'); // Name is optional $mail->addReplyTo('info@example.com', 'Information'); $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); //Attachments $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; }
我的例子:
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: // +---------------------------------------------------------------------- // 應用公共文件 // 郵箱 use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; function mailto($to, $nickname, $title, $content) { // 實例化 $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = 2; // Enable verbose debug output 2調試模式 0用戶模式 $mail->isSMTP(); // Set mailer to use SMTP $mail->CharSet = 'utf-8'; // 設置右鍵格式編碼 $mail->Host = 'smtp.qq.com'; // Specify main and backup SMTP servers smtp服務器的名稱 $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = '1225388159@qq.com'; // SMTP username $mail->Password = 'amltoqg*uuidhjff'; // SMTP password 此為QQ授權碼。 $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted 目前規(guī)定必須使用ssl,非ssl的協議已經不支持了 $mail->Port = 465; // TCP port to connect to ssl協議,端口號一般是465 //Recipients $mail->setFrom('1225388159@qq.com', 'Gether管理員'); // 設置右鍵發(fā)送人信息(郵箱, 昵稱) $mail->addAddress($to, $nickname); // 設置收件人信息(郵箱, 昵稱) // $mail->addAddress('ellen@example.com'); // Name is optional // $mail->addReplyTo('info@example.com', 'Information'); // $mail->addCC('cc@example.com'); // $mail->addBCC('bcc@example.com'); // // //Attachments 附件 // $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = $title; // 設置發(fā)送的郵件標題 $mail->Body = $content; // 設置郵件發(fā)送內容 // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; return $mail->send(); } catch (Exception $e) { exception($mail->ErrorInfo,1001); // 失敗拋出錯誤信息 } }
-
在你的PHP項目中,找到application/common.php創(chuàng)建公用的PHP函數。
use PHPMailer\PHPMailer\PHPMailer; // 引用 use PHPMailer\PHPMailer\Exception; function mailto($to, $nickname, $title, $content) // 傳入參數(收件人郵箱,收件人昵稱,郵件標題,郵件內容) { ··· }
這時候,跑起你的PHP程序~! :)
-
?。。?strong>盡然報錯了?!
-
坑點1:
Extension missing: openssl.
-
解決方法:
什么是openssl?
關于openssl,我說的不如百度百科齊全,還是看看百度百科的解釋吧!http://baike.baidu.com/view/300712.htm
php開啟openssl的方法,大多數情況下openssl是沒有開啟的,要想啟用需要進行下簡單的設置:
windows下開啟方法:
- 找到 php安裝目錄\PHPTutorial\php\對應版本\php.ini
- 首先檢查php.ini中
;extension=php_openssl.dll
是否存在, 如果存在的話去掉前面的注釋符‘;’, 如果不存在這行,那么添加extension=php_openssl.dll
。 - 將下的: php_openssl.dll, ssleay32.dll, libeay32.dll 3個文件拷貝到 WINDOWS\system32\ 文件夾下。
- 如果沒有 php_openssl.dll ,點擊下載 php_openssl.dll。
- 下載下來有很多個php_openssl,找到你相應php版本下面的php_openssl。
查看php版本可以用如下代碼:
<?php phpinfo(); ?>
找到相應版本之后,按照上面講的第二步來就可以了。
- 重啟apache或者iis,至此,openssl功能就開啟了。
-
-
坑點2:
SMTP connect() failed.
配置QQ郵件服務器和端口號:接收郵件服務器:pop.qq.com,使用SSL,端口號995
發(fā)送郵件服務器:smtp.qq.com,使用SSL,端口號465或587
賬戶名:您的QQ郵箱賬戶名(如果您是VIP帳號或Foxmail帳號,賬戶名需要填寫完整的郵件地址)
密碼: 查看下一步,使用授權碼登陸。
電子郵件地址:您的QQ郵箱的完整郵件地址1、什么是授權碼?
授權碼是QQ郵箱推出的,用于登錄第三方客戶端的專用密碼。
適用于登錄以下服務:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務。
溫馨提醒:為了你的帳戶安全,更改QQ密碼以及獨立密碼會觸發(fā)授權碼過期,需要重新獲取新的授權碼登錄。
2、怎么獲取授權碼?
先進入設置-》帳戶頁面找到入口,按照以下流程操作。
(1)選擇第一項,點擊“開啟”
[圖片上傳失敗...(image-265eaf-1535877689457)]
(2)驗證密保
[圖片上傳失敗...(image-8d2d51-1535877689457)]
(3)獲取授權碼
[圖片上傳失敗...(image-54e8d0-1535877689457)]
3、在第三方客戶端怎么設置?
在第三方客戶端的密碼框里面輸入16位授權碼進行驗證。
-
-
至此,就可以愉快的使用郵件功能了。
讓我們測試一下吧!
- 注冊
img
- 去郵箱查看一下吧
img
成功!成功!成功!成功!成功!
歡迎大家訪問我的主頁:西城Gether學習網
我的博客:淺作序
2018/9/2 上午10:19:01