Home > php教程 > php手册 > PHPMailer使用教程(PHPMailer发送邮件实例分析)

PHPMailer使用教程(PHPMailer发送邮件实例分析)

WBOY
Release: 2016-06-13 11:56:21
Original
1055 people have browsed it

php虽然提供了mail()函数,但并不好用,而PHPMailer是一个不错的邮件发送工具,使用起来也是非常简单!

使用PHPMailer发送邮件

复制代码 代码如下:


header("content-type:text/html;charset=utf-8");
ini_set("magic_quotes_runtime",0);
require 'class.phpmailer.php';
try {
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->CharSet='UTF-8'; //设置邮件的字符编码,这很重要,不然中文乱码
$mail->SMTPAuth = true; //开启认证
$mail->Port = 25;
$mail->Host = "smtp.163.com";
$mail->Username = "phpddt1990@163.com";
$mail->Password = "这是密码";
//$mail->IsSendmail(); //如果没有sendmail组件就注释掉,否则出现“Could not execute: /var/qmail/bin/sendmail ”的错误提示
$mail->AddReplyTo("phpddt1990@163.com","mckee");//回复地址
$mail->From = "phpddt1990@163.com";
$mail->FromName = "www.phpddt.com";
$to = "987044391@qq.com";
$mail->AddAddress($to);
$mail->Subject = "phpmailer测试标题";
$mail->Body = "

phpmail演示

这是php点点通(www.phpddt.com)对phpmailer的测试内容";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示,可以省略
$mail->WordWrap = 80; // 设置每行字符串的长度
//$mail->AddAttachment("f:/test.png"); //可以添加附件
$mail->IsHTML(true);
$mail->Send();
echo '邮件已发送';
} catch (phpmailerException $e) {
echo "邮件发送失败:".$e->errorMessage();
}
?>

打开我的qq邮件可以看到:

测试非常顺利:

从上图可以看出,PHPMailer是支持html格式发送,而且支持发送图片,附件!经过测试,对各种SMTP服务器兼容很好!

添加附件如果报错:

那是因为(set_magic_quotes_runtime())已经关闭。并且在PHP6中已经完全移除此特性。

你可以注释或者删除掉出错的行,或者是在set_magic_quotes_runtime()前面加@符号
或者是配置;error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED

PHPMailer及测试文件下载:phpmailer.rar

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