PHP:ThinkPHP使用PHPMailer发送html邮件

网友投稿 598 2022-05-29

文档

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

PHP:ThinkPHP使用PHPMailer发送html邮件

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小时内删除侵权内容。

上一篇:web前端学习分享-如何在vue中引入swiper及自动播放功能不能实现的问题
下一篇:【PMP】Head First PMP 学习笔记 第四章 项目整合管理
相关文章