使用 phpmailer 发送邮件

2020-01-09  本文已影响0人  苍松拔雨

1. 相关资料

composer require phpmailer/phpmailer

名称 地址
Packagist https://packagist.org/packages/phpmailer/phpmailer

2. phpmailer 成员

2.1 构造方法

$mail = new PHPMailer(true);

2.2 设置邮件服务的成员

成员名称 说明 示例
$mail->SMTPDebug SMTPDebug 设置 $mail->SMTPDebug = SMTP::DEBUG_SERVER
$mail->isSMTP() 使用 SMTP 协议 -
$mail->Host SMTP 服务器的地址 $mail->Host = 'smtp.example.com'
$mail->Username SMTP 服务的用户名 $mail->Username = 'user@example.com'
$mail->Password SMTP 服务的密码 $mail->Password = 'password'
$mail->Port SMTP 服务的端口 $mail->Port = 25
$mail->SMTPAuth 是否开启 SMTP 认证 $mail->SMTPAuth = true
$mail->SMTPSecure TLS 加密 $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS

2.3 设置收件人和发件人的成员

成员名称 说明 示例
$mail->setFrom() 设置发件人和发件人名称 $mail->setFrom('from@example.com', 'Mailer')
$mail->addAddress() 设置收件人 ,多次调用可以发给多个收件人 $mail->addAddress('joe@example.net', 'Joe User')
$mail->addReplyTo() 设置回复人 $mail->addReplyTo('info@example.com', 'Information')
$mail->addCC() 设置抄送人 $mail->addCC('cc@example.com')
$mail->addBCC() 设置密送人 $mail->addBCC('bcc@example.com')
$mail->clearAllRecipients() 清空收件人 -

2.4 设置邮件附件的成员

成员名称 说明 示例
$mail->addAttachment() 添加一个附件 ,可多次调用 $mail->addAttachment('/tmp/image.jpg', 'new.jpg')

2.5 设置邮件内容的成员

成员名称 说明 示例
$mail->isHTML() 设置邮件内容是否是 HTML 格式 $mail->isHTML(true)
$mail->Subject 设置邮件的主题 $mail->Subject = 'Here is the subject';
$mail->Body 设置邮件内容 ,HTML 格式使用 $mail->Body = 'This is the HTML message body <b>in bold!</b>'
$mail->AltBody 设置邮件内容 ,纯文本格式使用 $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'

2.6 发送邮件的成员

成员名称 说明 示例
$mail->send() 发送邮件 -
上一篇下一篇

猜你喜欢

热点阅读