Home > php教程 > php手册 > 使用PHPMailer实现邮件发送代码分享

使用PHPMailer实现邮件发送代码分享

WBOY
Release: 2016-06-06 20:18:34
Original
1547 people have browsed it

这篇文章主要介绍了使用PHPMailer实现邮件发送的代码,以及phpmailer下载链接,有需要的朋友可以参考下

发送邮件是常用的功能,LZ今天在项目中也碰到了,,特此分享一下。

首先,去下载PHPMailer

1、https://github.com/dwqs/PHPMailer

2、

下载之后,将文件解压到项目目录的对应位置,将class.phpmailer.php和class.smtp.php引入项目中,看代码:(解压的文件不要删除,否则不行)

// 必要导入
require("class.phpmailer.php");
require("class.smtp.php");
date_default_timezone_set('Asia/Shanghai');//设定时区东八区
$mail = new PHPMailer(); //建立邮件发送类

$address = "15602277510@163.com";
$mail->IsSMTP(); // 使用SMTP方式发送
$mail->CharSet ="UTF-8";//设置编码,否则发送中文乱码
$mail->Host = "smtp.qq.com"; // 您的企业邮局域名
$mail->SMTPAuth = true; // 启用SMTP验证功能
$mail->Username = "461147874@qq.com"; // 邮局用户名(请填写完整的email地址)
$mail->Password = "**********"; // 邮局密码

$mail->From = "461147874@qq.com"; //邮件发送者email地址
$mail->FromName = "dwqs";
$mail->AddAddress($address, "dwqs");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//$mail->AddReplyTo("", "");

//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式

$mail->Subject = "验证邮件"; //邮件标题
$mail->Body = "Hello,这是测试邮件"; //邮件内容
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略

if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo "Message sent!恭喜,邮件发送成功!";
}
?>

测试结果:

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template