这篇文章主要介绍了PHPMailer发送HTML内容、带附件的邮件实例,发送的内容包含图片和文字,附件则发送的一个EXCEL表,需要的朋友可以参考下
PHPMailer是一个封装好的PHP邮件发送类,支持发送HTML内容的电子邮件,以及可以添加附件发送,并不像PHP本身mail()函数需要服务器环境支持,您只需要设置邮件服务器以相关信息就能实现邮件发送功能。
本文将结合实例代码,讲解如何设置和实现发送HTML以及带附件的邮件功能。
首先,您可以到PHPMailer获取最新的下载包,解压到WEB目录下。
然后建立一个sendmail.php的文件,载入PHPMailer类,并设置相关属性参数,如邮件服务器地址,发件人和收件人,邮件内容等等,详情请看代码:
复制代码 代码如下:
require_once('class.phpmailer.php'); //载入PHPMailer类
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host = "smtp.163.com"; //SMTP服务器 以163邮箱为例子
$mail->Port = 25; //邮件发送端口
$mail->SMTPAuth = true; //启用SMTP认证
$mail->CharSet = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = "helloweba@163.com"; //你的邮箱
$mail->Password = "xxx"; //你的密码
$mail->Subject = "你好"; //邮件标题
$mail->From = "helloweba@163.com"; //发件人地址(也就是你的邮箱)
$mail->FromName = "月光光"; //发件人姓名
$address = "xyz@163.com";//收件人email
$mail->AddAddress($address, "亲");//添加收件人(地址,昵称)
$mail->AddAttachment('xx.xls','我的附件.xls'); // 添加附件,并指定名称
$mail->IsHTML(true); //支持html格式内容
$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //设置邮件中的图片
$mail->Body = '你好, 朋友!
这是一封来自
target="_blank">jb51.net的邮件!
'; //邮件主体内容
//发送
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
从代码中可以看出,实例化PHPMailer后,我们指定使用SMTP方式来发邮件,设置SMTP邮件服务器,,并启用SMTP认证,如果您的邮件服务器不需要认证,则设置$mail->SMTPAuth=false,并且不需要密码就可以发送。然后设置字符集和编码支持中文字符,注意原版的PHPMailer包对中文字符的支持不太理想,所以您可以下载helloweba示例中的改进包。然后设置发件人和收件人,添加附件。注意附件原名最好不要用中文,可以在AddAttachment()指定中文名称。然后设置邮件html内容,最后就是发送,流程一目了然,
如果发送成功,将会收到如下邮件:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function