最近需要用到发送邮件的功能,原本是用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 = "bkjia@163.com"; // 用户名
$mail->Password = "yourpassword"; // 密码
//$body = file_get_contents('application/views/nmra/register.html');
//$body = preg_replace('/\\/','', $body); //Strip backslashes
$body = '<p><body style="margin: 10px;"></p>';
$body .= '<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 14px; ">';
$body .= '<div align="center"><img src="images/phpmailer.gif" style="max-width:90%" alt="PHP发送邮件类库PHPMailer的简单使用_PHP教程" ></div>';
$body .= '<p>'.$nickname.',您好。</p>';
$body .= '<p>恭喜你成为帮客之家研究协会的第'.$id.'名会员。</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1549">
<img src="https://img.php.cn/upload/ai_manual/000/969/633/68b7a0dc42b0b527.png" alt="Felvin">
</a>
<div class="aritcle_card_info">
<a href="/ai/1549">Felvin</a>
<p>AI无代码市场,只需一个提示快速构建应用程序</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="Felvin">
<span>161</span>
</div>
</div>
<a href="/ai/1549" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="Felvin">
</a>
</div>
';
$body .= '<p>帮客之家社区(bangke Research Association)是一个程序猿、攻城狮、设计狮和开发者们技术交流、话题讨论的社区。希望在这里你能找到感兴趣的话题与志同道合的朋友。</p>';
$body .= '请点击以下链接验证您的邮箱,请注意域名为bkjia.com:<a href="http://www.bkjia.com/librarys/accounts/activation/?code="'.$activation_code.'" target="_blank">http://www.bkjia.com/librarys/accounts/activation/?code='.$activation_code.'</a>';
$body .= '<p>顺祝工作学习愉快,生活舒心。</p>';
$body .= '</div></body>';
//echo $body;
$mail->AddReplyTo("bkjia@163.com","Gonn");
$mail->SetFrom('bkjia@163.com', 'Gonn');
$mail->AddReplyTo("bkjia@163.com","Gonn");
$address = "252211974@qq.com";
//$address = "bkjia@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 是一个功能强大的邮件类,其主要功能特点:
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号