允许转载,但请注明出处:http://blog.csdn.net/sysuzjz/article/details/41826927
较常用配置如下:
require("PHPMailer_5.2.4/class.phpmailer.php");
$mail = new phpmailer();
$mail->SMTPDebug = true; // 设置是否输出SMTP调试信息
$mail->isSMTP(); // 使用SMTP发送
$mail->Priority = 3; // 邮件优先级, 1高,3普通,5低
$mail->CharSet = "utf-8"; // 定义邮件编码
$mail->ContentType = "text/plain"; // 文档类型
$mail->Host = "smtp.163.com"; // 邮件服务器地址
$mail->SMTPAuth = true; // 开启SMTP认证
$mail->Username = 'user@example.com'; // 登陆SMTP用户名
$mail->Password = 'secret'; // 登陆SMTP密码
$mail->SMTPSecure = 'tls'; // SMTP加密方式,可选"tls"和"ssl"
$mail->Port = 587; // 连接的TCP端口
$mail->Encoding = "base64"; // 邮件的编码方式,可选:"8bit","7bit","binary","base64",和"quoted-printable"
$mail->From = 'from@example.com'; // 发件人邮箱
$mail->FromName = 'Mailer'; // 发件人姓名
$mail->addAddress('joe@example.net', 'Joe User'); // 增加收件人地址,参数一为收件人邮箱,参数二可选,为收件人姓名
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information'); // 添加回复标签,参数同addAddress
$mail->addCC('cc@example.com'); // 添加抄送,参数同addAddress
$mail->addBCC('bcc@example.com'); // 添加密送,参数同addAddress
$mail->addAttachment('/var/tmp/file.tar.gz'); // 增加附件,参数一为附件路径,必选,其他参数可选,分别为名称,编码,类型,默认为$name = '', $encoding = 'base64', $type = 'application/octet-stream'
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; // 邮件内容,HTML(如果isHTML为true)或text(如果isHTML为false)
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; // 当邮件不支持HTML格式显示的文本
if($mail->IsError() || !$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}