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

WBOY
Release: 2016-05-16 20:34:22
Original
856 people have browsed it

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

首先,去下载PHPMailer

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

2、http://download.csdn.net/detail/u011043843/8063583

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

  // 必要导入<br> require("class.phpmailer.php");<br> require("class.smtp.php");<br> date_default_timezone_set('Asia/Shanghai');//设定时区东八区<br>$mail = new PHPMailer(); //建立邮件发送类<br><br>$address = "15602277510@163.com";<br>$mail->IsSMTP(); // 使用SMTP方式发送<br>$mail->CharSet ="UTF-8";//设置编码,否则发送中文乱码<br>$mail->Host = "smtp.qq.com"; // 您的企业邮局域名<br>$mail->SMTPAuth = true; // 启用SMTP验证功能<br>$mail->Username = "461147874@qq.com"; // 邮局用户名(请填写完整的email地址)<br>$mail->Password = "**********"; // 邮局密码<br><br>$mail->From = "461147874@qq.com"; //邮件发送者email地址<br>$mail->FromName = "dwqs";<br>$mail->AddAddress($address, "dwqs");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")<br>//$mail->AddReplyTo("", "");<br><br>//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件<br>//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式<br><br>$mail->Subject = "验证邮件"; //邮件标题<br>$mail->Body = "Hello,这是测试邮件"; //邮件内容<br>$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略<br><br>if(!$mail->Send()) {<br>    echo 'Mailer Error: ' . $mail->ErrorInfo;<br>  } else {<br>  echo "Message sent!恭喜,邮件发送成功!";<br>  }<br>?>
Copy after login

测试结果:

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template