PHP邮件发送类PHPMailer用法实例详解


Posted in PHP onSeptember 22, 2014

本文实例讲述了PHP邮件发送类PHPMailer用法,并详细讲述了其具体的操作步骤。分享给大家供大家参考。具体步骤如下:

1.在服务器安装 sendmail

sudo apt-get install sendmail

2.启动 sendmail

sudo /etc/init.d/sendmail start

3.修改 php.ini

[mail function] 
SMTP = localhost 
smtp_port = 25 
sendmail_from = me@example.com

4.Function sendMail函数如下

<?php 
/* 调用PHPMailer发送电邮 
* @param String $receiver   收件人 
* @param String $sender    发件人 
* @param String $sender_name 发件人名称如为空则用发件人地址代替 
* @param String $subject   邮件主题 
* @param String $content   邮件内容 
* @param boolean $ishtml    是否html电邮 
* @param Array  $attachements 附件 
* @return boolean 
*/ 
function sendMail($receiver, $sender, $sender_name, $subject, $content, $ishtml=true, $attachments=array()) { 
  include_once "class-phpmailer.php";  
 
  if(empty($receiver) || empty($sender) || empty($subject) || empty($content)){ 
    return false; 
  } 
   
  $mail = new PHPMailer();  
 
  //$mail->IsSMTP();        // 经smtp发送  
  //$mail->Host = "smtp.gmail.com"; // SMTP 服务器 
  //$mail->Port = 465;       // SMTP 端口 
  //$mail->SMTPSecure = 'ssl';   // 加密方式 
  //$mail->SMTPAuth = true;     // 打开SMTP认证 
  //$mail->Username = "username";  // 用户名 
  //$mail->Password = "password";  // 密码 
 
  $mail->IsMail();         // using PHP mail() function 有可能??霈F?封?件可能不是由以下使用者所?魉偷奶崾 
       
  $mail->From = $sender;      // 发信人  
  $mail->FromName = $sender_name;  // 发信人别名  
  $mail->AddReplyTo($sender);    // 回覆人 
  $mail->AddAddress($receiver);   // 收信人  
 
  // 以html方式发送 
  if($ishtml){ 
    $mail->IsHTML(true); 
  } 
 
  // 发送附件 
  if($attachments){ 
    if(is_array($attachments)){ 
      $send_attachments = array(); 
 
      $tmp_attachments = array_slice($attachments,0,1); 
      if(!is_array(array_pop($tmp_attachments))){ 
        if(isset($attachments['path'])){ 
          array_push($send_attachments, $attachments);           
        }else{ 
          foreach($attachments as $attachment){ 
            array_push($send_attachments, array('path'=>$attachment)); 
          } 
        } 
      }else{ 
        $send_attachments = $attachments; 
      } 
 
      foreach($send_attachments as $attachment){ 
        $attachment['name'] = isset($attachment['name'])? $attachment['name'] : null; 
        $attachment['encoding'] = isset($attachment['encoding'])? $attachment['encoding'] : 'base64'; 
        $attachment['type'] = isset($attachment['type'])? $attachment['type'] : 'application/octet-stream'; 
        if(isset($attachment['path']) && file_exists($attachment['path'])){ 
          $mail->AddAttachment($attachment['path'],$attachment['name'],$attachment['encoding'],$attachment['type']); 
        } 
      } 
    }elseif(is_string($attachments)){ 
      if(file_exists($attachments)){ 
        $mail->AddAttachment($attachments); 
      } 
    } 
  } 
 
  $mail->Subject = $subject; // 邮件标题 
  $mail->Body   = $content; // 邮件?热 
  return $mail->Send();  
} 
 
// DEMO示例如下: 
$receiver = 'receiver@test.com'; 
$sender = 'sender@test.com'; 
$sender_name = 'sender name'; 
$subject = 'subjecct'; 
$content = 'content'; 
 
// 四种格式都可以 
$attachments = 'attachment1.jpg'; 
$attachments = array('path'=>'attachment1.jpg', 'name'=>'附件1.jpg'); 
$attachments = array('attachment1.jpg','attachment2.jpg','attachment3.jpg'); 
$attachments = array( 
  array('path'=>'attachment1.jpg', 'name'=>'附件1.jpg'), 
  array('path'=>'attachment2.jpg', 'name'=>'附件2.jpg'), 
  array('path'=>'attachment3.jpg', 'name'=>'附件3.jpg'), 
); 
$flag = sendMail($receiver, $sender, $sender_name, $subject, $content, true, $attachments); 
echo $flag; 
?>

源码点击此处本站下载。

希望本文所述对大家PHP程序设计的学习有所帮助。

PHP 相关文章推荐
PHP.MVC的模板标签系统(四)
Sep 05 PHP
自动跳转中英文页面
Oct 09 PHP
php设计模式 Delegation(委托模式)
Jun 26 PHP
php语言流程控制中的主动与被动
Nov 05 PHP
深入apache host的配置详解
Jun 09 PHP
PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解
Jul 03 PHP
PHP实现定时执行任务的方法
Oct 05 PHP
php遍历类中包含的所有元素的方法
May 12 PHP
smarty简单应用实例
Nov 03 PHP
PHP实现自动识别原编码并对字符串进行编码转换的方法
Jul 13 PHP
PHP数据库操作二:memcache用法分析
Aug 16 PHP
php输出控制函数和输出函数生成静态页面
Jun 27 PHP
php实现的CSS更新类实例
Sep 22 #PHP
php的XML文件解释类应用实例
Sep 22 #PHP
php实现的返回数据格式化类实例
Sep 22 #PHP
php实现的替换敏感字符串类实例
Sep 22 #PHP
php实现的发送带附件邮件类实例
Sep 22 #PHP
PHP实现AES256加密算法实例
Sep 22 #PHP
php生成QRcode实例
Sep 22 #PHP
You might like
PHP个人网站架设连环讲(二)
2006/10/09 PHP
基于asp+ajax和数据库驱动的二级联动菜单
2010/05/06 PHP
基于PHP实现简单的随机抽奖小程序
2016/01/05 PHP
PHP文件上传处理案例分析
2016/10/15 PHP
PHP实现二维数组按照指定的字段进行排序算法示例
2019/04/23 PHP
JS小框架 fly javascript framework
2009/11/26 Javascript
jquery获取选中的文本和值的方法
2014/07/08 Javascript
js通过location.search来获取页面传来的参数
2014/09/11 Javascript
Nodejs从有门道无门菜鸟起飞必看教程
2016/07/20 NodeJs
vue router路由嵌套不显示问题的解决方法
2017/06/17 Javascript
JS控制下拉列表左右选择实例代码
2020/05/08 Javascript
如何将Node.js中的回调转换为Promise
2020/11/10 Javascript
[48:24]完美世界DOTA2联赛PWL S3 Forest vs INK ICE 第一场 12.09
2020/12/12 DOTA
Python使用迭代器捕获Generator返回值的方法
2017/04/05 Python
HTML中使用python屏蔽一些基本功能的方法
2017/07/07 Python
Python爬取数据并写入MySQL数据库的实例
2018/06/21 Python
把csv文件转化为数组及数组的切片方法
2018/07/04 Python
Python3 关于pycharm自动导入包快捷设置的方法
2019/01/16 Python
mac在matplotlib中显示中文的操作方法
2020/03/06 Python
Python matplotlib可视化实例解析
2020/06/01 Python
python学习将数据写入文件并保存方法
2020/06/07 Python
解决pycharm debug时界面下方不出现step等按钮及变量值的问题
2020/06/09 Python
Python实现ElGamal加密算法的示例代码
2020/06/19 Python
Python 代码调试技巧示例代码
2020/08/11 Python
澳大利亚家具和家居用品在线商店:Interiors Online
2018/03/05 全球购物
美国在线打印网站:Overnight Prints
2018/10/11 全球购物
怀旧香味蜡烛:Homesick
2019/11/02 全球购物
工程造价自荐信
2013/10/09 职场文书
助人为乐表扬信范文
2014/01/14 职场文书
打架检讨书300字
2014/02/02 职场文书
大学生党员自我剖析材料
2014/10/06 职场文书
2014最新股权信托合同协议书
2014/11/18 职场文书
2015年上半年计生工作总结
2015/03/30 职场文书
幼儿园食品安全责任书
2015/05/08 职场文书
承诺书的内容有哪些,怎么写?
2019/06/21 职场文书
Python 中面向接口编程
2022/05/20 Python