最近需要用到发送邮件的功能,原本是用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 .= '
';
//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!";
}
}