当前位置:Gxlcms > PHP教程 > 关于PHPMailer-PHPemailtransportclass的相关讲解

关于PHPMailer-PHPemailtransportclass的相关讲解

时间:2021-07-01 10:21:17 帮助过:5人阅读

在服务器安装 sendmail

  1. sudo apt-get install sendmail

启动 sendmail

  1. sudo /etc/init.d/sendmail start

修改 php.ini

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

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(empty($receiver) || empty($sender) || empty($subject) || empty($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. // 邮件标题
  66. $mail->Body = $content;
  67. // 邮件內容
  68. return $mail->Send();
  69. }
  70. // DEMO
  71. $receiver = 'receiver@test.com';
  72. $sender = 'sender@test.com';
  73. $sender_name = 'sender name';
  74. $subject = 'subjecct';
  75. $content = 'content';
  76. // 四種格式都可以
  77. $attachments = 'attachment1.jpg';
  78. $attachments = array('path'=>'attachment1.jpg', 'name'=>'附件1.jpg');
  79. $attachments = array('attachment1.jpg','attachment2.jpg','attachment3.jpg');
  80. $attachments = array(
  81. array('path'=>'attachment1.jpg', 'name'=>'附件1.jpg'),
  82. array('path'=>'attachment2.jpg', 'name'=>'附件2.jpg'),
  83. array('path'=>'attachment3.jpg', 'name'=>'附件3.jpg'),
  84. );
  85. $flag = sendMail($receiver, $sender, $sender_name, $subject, $content, true, $attachments);
  86. echo $flag;
  87. ?>

本文讲解了关于PHPMailer - PHP email transport class 的相关讲解,更多相关内容请关注Gxl网。

相关推荐:

关于PHP 遍历文件夹及文件类及处理类 的理解

讲解RewriteCond和13个mod_rewrite应用举例Apache伪静态 的相关知识

了解正向代理与反向代理的区别

以上就是关于PHPMailer - PHP email transport class 的相关讲解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行