## 一.使用以前下載的phpmailer.zip Sdk包(比較麻煩)
1. extend目錄下創建一個phpmailer文件夾
```linux
cd extend
mkdir phpmailer
```
2. 解壓phpmailer.zip,把里面的文件拷貝或移動到extend/phpmailer目錄下
```linux
tar -zxvf phpmailer.zip
mv -r phpmailer extend
```
3. 修改類名
class.phpmailer.php 改為PHPMailer class.smtp.php改為SMTP
4. 改命名空間
PHPMailer.php、SMTP.php中添加 namespace phpmailer;
并且在PHPMailer.php中,use phpmailer\SMTP; 同時把底下的phpmailerException繼承的Exception前添加 \
5.創建一個SendEmail.php類,封裝具體的發送郵件方法
```php
<?php
namespace phpmailer;
class SendEmail
{
//發送郵件方法 $to發送給誰, $title郵件標題 , $content郵件內容
//同時把相關的配置信息保存在application/extra/email.php中
public static function send($to, $title, $content)
{
if (empty($to)) {
return false;
}
date_default_timezone_set('PRC');//set time
$mail = new PHPMailer;
$mail->isSMTP();
//$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = config('mail.host');
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = config('mail.port');
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = config('mail.username');
//Password to use for SMTP authentication
$mail->Password = config('mail.password');
//Set who the message is to be sent from
$mail->setFrom(config('mail.username'), config('mail.nickname'));
$mail->addAddress($to);
$mail->Subject = $title;
$mail->msgHTML($content);
//send the message, check for errors
if (!$mail->send()) {
return false;
} else {
return true;
}
}
}
```
application/extra/email.php文件
```php
<?php
return [
//qq郵箱配置
'host' => 'smtp.qq.com', //qq郵箱smtp發送服務器地址
'port' => 587, //qq郵箱smtp發送端口號
'username' => '*********@qq.com', //配置郵箱的發送者
'password' => '密鑰', //smtp下的密鑰
'nickname' => '陽光男孩' //發送者昵稱
];
```
詳細使用可以參考菜鳥教程下的phpmailer使用方法:[地址]([https://www.runoob.com/w3cnote/php-phpmailer.html](https://www.runoob.com/w3cnote/php-phpmailer.html))
6. 控制器中發送郵件
```php
...
public function send()
{
$res = \phpmailer\Email::send('57******35@qq.com','測試郵件標題','這是一封測試專用郵件,請忽略');
if ($res) {
return json(['code'=>200,'msg'=>"發送成功"]);
}
}
```
## 使用phpmailer提供的composer包
1. 使用composer下載phpmailer
```php
composer require phpmailer/phpmailer
```
2. 在應用公共函數common.php寫個發送郵件的函數(騰訊郵箱的為例),需要注意的是實例化PHPMailer的時候。另外可以在application/extra下單獨配置一個email.php文件存放配置信息
```php
/**
* 發送郵件
* @param string $tomail 接收郵件者郵箱
* @param string $name 接收郵件者名稱
* @param string $subject 郵件主題
* @param string $body 郵件內容
* @param string $attachment 附件列表
* @return boolean
* @throws phpmailerException
*/
function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) {
$mail = new PHPMailer\PHPMailer\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"; // SMTP 服務器
$mail->Port = 465; // SMTP服務器的端口號
$mail->Username = "XXXXX@qq.com"; // SMTP服務器用戶名
$mail->Password = "XXXXX"; // SMTP服務器密碼,這里是你開啟SMTP服務時生成密碼
$mail->SetFrom('XXXXX@qq.com', '發件人昵稱');
$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;
}
```
3. 在相對應的控制器中調用前面設置的send_mail方法
```php
...
public function send_email()
{
$toemail='57******35@qq.com';//收件人郵箱
$name='親愛的57******35@qq.com,您好!'; //接收郵件者名稱
$subject='郵件標題'; //郵件主題
$content='恭喜你,郵件測試成功。'; //郵件內容
//調用方法發送郵件
dump(send_mail($toemail,$name,$subject,$content));
}
```