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

这篇文章主要介绍了PHP邮件发送类PHPMailer用法,详细的讲述了安装及配置的方法与功能实现代码,非常具有实用价值,需要的朋友可以参考下

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

1.在服务器安装 sendmail

sudo apt-get install sendmail

2.启动 sendmail

sudo /etc/init.d/sendmail start

3.修改 php.ini

  1. [mail function]
  2. SMTP = localhost
  3. smtp_port = 25
  4. sendmail_from = me@example.com

4.Function sendMail函数如下

  1. <?php
  2. /* 调用PHPMailer发送电邮
  3. * @param String $receiver 收件人
  4. * @param String $sender 发件人
  5. * @param String $sender_name 发件人名称如为空则用发件人地址代替
  6. * @param String $subject 邮件主题
  7. * @param String $content 邮件内容
  8. * @param boolean $ishtml 是否html电邮
  9. * @param Array $attachements 附件
  10. * @return boolean
  11. */
  12. function sendMail($receiver, $sender, $sender_name, $subject, $content, $ishtml=true, $attachments=array()) {
  13. include_once "class-phpmailer.php";
  14. if(emptyempty($receiver) || emptyempty($sender) || emptyempty($subject) || emptyempty($content)){
  15. return false;
  16. }
  17. $mail = new PHPMailer();
  18. //$mail->IsSMTP(); // 经smtp发送
  19. //$mail->Host = "smtp.gmail.com"; // SMTP 服务器
  20. //$mail->Port = 465; // SMTP 端口
  21. //$mail->SMTPSecure = 'ssl'; // 加密方式
  22. //$mail->SMTPAuth = true; // 打开SMTP认证
  23. //$mail->Username = "username"; // 用户名
  24. //$mail->Password = "password"; // 密码
  25. $mail->IsMail(); // using PHP mail() function 有可能會出現這封郵件可能不是由以下使用者所傳送的提示
  26. $mail->From = $sender; // 发信人
  27. $mail->FromName = $sender_name; // 发信人别名
  28. $mail->AddReplyTo($sender); // 回覆人
  29. $mail->AddAddress($receiver); // 收信人
  30. // 以html方式发送
  31. if($ishtml){
  32. $mail->IsHTML(true);
  33. }
  34. // 发送附件
  35. if($attachments){
  36. if(is_array($attachments)){
  37. $send_attachments = array();
  38. $tmp_attachments = array_slice($attachments,0,1);
  39. if(!is_array(array_pop($tmp_attachments))){
  40. if(isset($attachments['path'])){
  41. array_push($send_attachments, $attachments);
  42. }else{
  43. foreach($attachments as $attachment){
  44. array_push($send_attachments, array('path'=>$attachment));
  45. }
  46. }
  47. }else{
  48. $send_attachments = $attachments;
  49. }
  50. foreach($send_attachments as $attachment){
  51. $attachment['name'] = isset($attachment['name'])? $attachment['name'] : null;
  52. $attachment['encoding'] = isset($attachment['encoding'])? $attachment['encoding'] : 'base64';
  53. $attachment['type'] = isset($attachment['type'])? $attachment['type'] : 'application/octet-stream';
  54. if(isset($attachment['path']) && file_exists($attachment['path'])){
  55. $mail->AddAttachment($attachment['path'],$attachment['name'],$attachment['encoding'],$attachment['type']);
  56. }
  57. }
  58. }elseif(is_string($attachments)){
  59. if(file_exists($attachments)){
  60. $mail->AddAttachment($attachments);
  61. }
  62. }
  63. }
  64. $mail->Subject = $subject; // 邮件标题
  65. $mail->Body = $content; // 邮件內容
  66. return $mail->Send();
  67. }
  68. // DEMO示例如下:
  69. $receiver = 'receiver@test.com';
  70. $sender = 'sender@test.com';
  71. $sender_name = 'sender name';
  72. $subject = 'subjecct';
  73. $content = 'content';
  74. // 四种格式都可以
  75. $attachments = 'attachment1.jpg';
  76. $attachments = array('path'=>'attachment1.jpg', 'name'=>'附件1.jpg');
  77. $attachments = array('attachment1.jpg','attachment2.jpg','attachment3.jpg');
  78. $attachments = array(
  79. array('path'=>'attachment1.jpg', 'name'=>'附件1.jpg'),
  80. array('path'=>'attachment2.jpg', 'name'=>'附件2.jpg'),
  81. array('path'=>'attachment3.jpg', 'name'=>'附件3.jpg'),
  82. ); //www.phpfensi.com
  83. $flag = sendMail($receiver, $sender, $sender_name, $subject, $content, true, $attachments);
  84. echo $flag;
  85. ?>

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