PHP使用PHPMailer发送邮件的简单使用方法

2015-01-24信息快讯网

本文是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 = '<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="height: 90px; width: 340px"></div>';
 $body .= '<p>'.$nickname.',您好。</p>';
 $body .= '<p>恭喜你成为简明现代魔法研究协会的第'.$id.'名会员。</p>';
 $body .= '<p>现代魔法研究协会(NowaMagic Research Association)是一个程序猿、攻城狮、设计狮和开发者们技术交流、话题讨论的社区。希望在这里你能找到感兴趣的话题与志同道合的朋友。</p>';
 $body .= '请点击以下链接验证您的邮箱,请注意域名为nowamagic.net:<a href="http://www.nowamagic.net/librarys/accounts/activation/?code="'.$activation_code.'" target="_blank">http://www.nowamagic.net/librarys/accounts/activation/?code='.$activation_code.'</a>';
 $body .= '<p>顺祝工作学习愉快,生活舒心。</p>';
 $body .= '</div></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 等平台测试成功
提供的下载文件中,包括内容详细的说明文档及示
浅析ThinkPHP中execute和query方法的区别
php smarty truncate UTF8乱码问题解决办法
Codeigniter上传图片出现“You did not select a file to upload”错误解决办法
CodeIgniter框架过滤HTML危险代码
让CodeIgniter的ellipsize()支持中文截断的方法
Codeigniter实现多文件上传并创建多个缩略图
PHP+ajaxfileupload+jcrop插件完美实现头像上传剪裁
浅谈php扩展imagick
mac下Apache + MySql + PHP搭建网站开发环境
在PHP中使用X-SendFile头让文件下载更快
模板引擎smarty工作原理以及使用示例
Smarty模板学习笔记之Smarty简介
ThinkPHP使用心得分享-上传类UploadFile的使用
php中curl和file_get_content的区别
phpmailer中文乱码问题的解决方法
PHPMailer的主要功能特点和简单使用说明
PHP 利用Mail_MimeDecode类提取邮件信息示例
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
php mail to 配置详解
php使用exec shell命令注入的方法讲解
PHP迭代器的内部执行过程详解
PHP使用SOAP调用.net的WebService数据
PHP imagegrabscreen和imagegrabwindow(截取网站缩略图)的实例代码
PHP中spl_autoload_register函数的用法总结
Php header()函数语法及使用代码
mac下安装nginx和php
is_uploaded_file函数引发的不能上传文件问题
单点登录 Ucenter示例分析
php header功能的使用
php while循环得到循环次数
PHP file_get_contents设置超时处理方法
ThinkPHP利用PHPMailer实现邮件发送实现代码
php读取本地文件常用函数(fopen与file_get_contents)
PHP函数preg_match_all正则表达式的基本使用详细解析
完美解决PHP中的Cannot modify header information 问题
解析php中用PHPMailer来发送邮件的示例(126.com的例子)
解析yahoo邮件用phpmailer发送的实例
163的邮件用phpmailer发送(实例详解)
phpmailer发送gmail邮件实例详解
©2014-2024 dbsqp.com