来源:http://www.ido321.com/1103.html
发送邮件是常用的功能,LZ今天在项目中也碰到了,特此分享一下。
首先,去下载PHPMailer
1、https://github.com/dwqs/PHPMailer
2、http://download.csdn.net/detail/u011043843/8063583
下载之后,将文件解压到项目目录的对应位置,将class.phpmailer.php和class.smtp.php引入项目中,看代码:(解压的文件不要删除,否则不行)
<span><?php <span>// 必要导入</span> <span>require</span>(<span>"class.phpmailer.php"</span>); <span>require</span>(<span>"class.smtp.php"</span>); date_default_timezone_set(<span>'Asia/Shanghai'</span>);<span>//设定时区东八区</span> $mail = <span>new</span> PHPMailer(); <span>//建立邮件发送类</span> $address = <span>"15602277510@163.com"</span>; $mail->IsSMTP(); <span>// 使用SMTP方式发送</span> $mail->CharSet =<span>"UTF-8"</span>;<span>//设置编码,否则发送中文乱码</span> $mail->Host = <span>"smtp.qq.com"</span>; <span>// 您的企业邮局域名</span> $mail->SMTPAuth = <span>true</span>; <span>// 启用SMTP验证功能</span> $mail->Username = <span>"461147874@qq.com"</span>; <span>// 邮局用户名(请填写完整的email地址)</span> $mail->Password = <span>"**********"</span>; <span>// 邮局密码</span> $mail->From = <span>"461147874@qq.com"</span>; <span>//邮件发送者email地址</span> $mail->FromName = <span>"dwqs"</span>; $mail->AddAddress($address, <span>"dwqs"</span>);<span>//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")</span> <span>//$mail->AddReplyTo("", "");</span> <span>//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件</span> <span>//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式</span> $mail->Subject = <span>"验证邮件"</span>; <span>//邮件标题</span> $mail->Body = <span>"Hello,这是测试邮件"</span>; <span>//邮件内容</span> $mail->AltBody = <span>"This is the body in plain text for non-HTML mail clients"</span>; <span>//附加信息,可以省略</span> <span>if</span>(!$mail->Send()) { <span>echo</span> <span>'Mailer Error: '</span> . $mail->ErrorInfo; } <span>else</span> { <span>echo</span> <span>"Message sent!恭喜,邮件发送成功!"</span>; } ?></span>
测试结果:
下一篇:你的大学,有多少遗憾?
服务器没有SMTP