本文是php使用phpmailer发送邮件的简单使用方法,提供自己写的代码
最近需要用到发送邮件的功能,原本是用php自带的mail()函数发送的。php mail()这个方法非常简单、方便、易用,但是除了网易邮箱、qq邮箱、gmail邮箱等常用的邮箱可以收到之外,经测试hotmail、tom、live等邮箱是收不到此类邮件的。所以就转而使用phpmailer这个强大的邮件发送类。
使用官方自带的一些例子,有些会报 mailer error: could not instantiate mail function. 这个错误。参考了一些资料之后,,还是自己写了一个方法。代码很简单,就不多解释了。
复制代码 代码如下:
function mailto($nickname, $address, $id, $activation_code)
{
date_default_timezone_set('prc');
include_once(class.phpmailer.php);
$mail = new phpmailer(); // defaults to using php mail()
$mail->issmtp();
$mail->host = smtp.163.com; // smtp 服务器
$mail->smtpauth = true; // 打开smtp 认证
$mail->username = nowamagic@163.com; // 用户名
$mail->password = yourpassword; // 密码
//$body = file_get_contents('application/views/nmra/register.html');
//$body = preg_replace('/\\\\/','', $body); //strip backslashes
$body = '
';
$body .= '';
$body .= '
';
$body .= ''.$nickname.',您好。
';
$body .= '恭喜你成为简明现代魔法研究协会的第'.$id.'名会员。
';
$body .= '现代魔法研究协会(nowamagic research association)是一个程序猿、攻城狮、设计狮和开发者们技术交流、话题讨论的社区。希望在这里你能找到感兴趣的话题与志同道合的朋友。
';
$body .= '请点击以下链接验证您的邮箱,请注意域名为nowamagic.net:?code='.$activation_code.'';
$body .= '顺祝工作学习愉快,生活舒心。
';
$body .= '
';
//echo $body;
$mail->addreplyto(nowamagic@163.com,gonn);
$mail->setfrom('nowamagic@163.com', 'gonn');
$mail->addreplyto(nowamagic@163.com,gonn);
$address = 252211974@qq.com;
//$address = nowamagic@gmail.com;
$mail->addaddress($address, $nickname);
$subject = 收到来自简明现代魔法的邮件;
$mail->subject = =?utf-8?b?.base64_encode($subject).?=;
// optional, comment out and test
$mail->altbody = to view the message, please use an html compatible email viewer!;
$mail->msghtml($body);
//$mail->addattachment(images/phpmailer.gif); // attachment
//$mail->addattachment(images/phpmailer_mini.gif); // attachment
if(!$mail->send()) {
//echo mailer error: . $mail->errorinfo;
}
else {
//echo message sent!;
}
}
使用的时候只要引入两个php类,然后自己写个方法就ok了,两个类很小,发送邮件速度也很快。
phpmailer 是一个功能强大的邮件类,其主要功能特点:
支持邮件 s/mime加密的数字签名
支持邮件多个 tos, ccs, bccs and reply-tos
可以工作在任何服务器平台,所以不用担心win平台无法发送邮件的问题的
支持文本/html格式邮件
可以嵌入image图像
对于邮件客户端不支持html阅读的进行支持
功能强大的发送邮件调试功能debug
自定义邮件header
冗余smtp服务器支持
支持8bit, base64, binary, and quoted-printable 编码
文字自动换行
支持多附件发送功能
支持smtp服务器验证功能
在sendmail, qmail, postfix, gmail, imail, exchange 等平台测试成功
提供的下载文件中,包括内容详细的说明文档及示