PHP:ThinkPHP使用PHPMailer發送html郵件
文檔
https://github.com/PHPMailer/PHPMailer
安裝
composer require phpmailer/phpmailer
1
代碼示例
配置文件 config/mail.php
'smtp.163.com', 'MAIL_PORT' => 465, 'MAIL_USERNAME' => 'xxx@163.com', 'MAIL_PASSWORD' => 'xxxxxx', 'MAIL_FROM_NAME' => '山中無老虎', ];
1
2
3
4
5
6
7
8
9
10
11
12
13
CharSet = "UTF-8"; //設定郵件編碼 $mail->SMTPDebug = SMTP::DEBUG_OFF; // 調試模式輸出 $mail->isSMTP(); // 使用SMTP // SMTP服務器 $mail->Host = config('mail.MAIL_HOST'); // 服務器端口 25 或者465 具體要看郵箱服務器支持 $mail->Port = config('mail.MAIL_PORT'); $mail->SMTPAuth = true; // 允許 SMTP 認證 $mail->Username = config('mail.MAIL_USERNAME'); // SMTP 用戶名 即郵箱的用戶名 $mail->Password = config('mail.MAIL_PASSWORD'); // SMTP 密碼 部分郵箱是授權碼(例如163郵箱) $mail->SMTPSecure = 'ssl'; // 允許 TLS 或者ssl協議 //發件人 $mail->setFrom(config('mail.MAIL_USERNAME'), config('mail.MAIL_FROM_NAME')); // 收件人 foreach ($address_list as $address) { $mail->addAddress($address); } //Content $mail->isHTML(true); // 是否以HTML文檔格式發送 發送后客戶端可直接顯示對應HTML內容 $mail->Subject = $subject; $mail->Body = $body; $mail->send(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
測試
http)->run(); use app\service\MailService; use PHPUnit\Framework\TestCase; class MailServiceTest extends TestCase { /** * @doesNotPerformAssertions */ public function testSend() { MailService::send('標題', '內容', ['xxx@qq.com']); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
如果需要發送內容更豐富的html,可以引入模板引擎去渲染郵件內容
PHP:ThinkPHP使用Twig渲染html
參考
PHP 使用 phpmailer 發送電子郵件
HTML PHP ThinkPHP
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。