PHP语言Mailer - PHP email transport class
小标 2018-07-12 来源 : 阅读 1013 评论 0

摘要:本文主要向大家介绍了PHP语言Mailer - PHP email transport class,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言Mailer - PHP email transport class,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

在服务器安装 sendmail

[plain] view plain copy

1. sudo apt-get install sendmail  

启动 sendmail

[plain] view plain copy

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

修改 php.ini

[plain] view plain copy

1. [mail function]  

2. SMTP = localhost  

3. smtp_port = 25  

4. sendmail_from = me@example.com  

Function sendMail

[php] view plain copy

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.   

15.     if(empty($receiver) || empty($sender) || empty($subject) || empty($content)){  

16.         return false;  

17.     }  

18.       

19.     $mail = new PHPMailer();    

20.   

21.     //$mail->IsSMTP();                // 经smtp发送   

22.     //$mail->Host = "smtp.gmail.com"; // SMTP 服务器  

23.     //$mail->Port = 465;              // SMTP 端口  

24.     //$mail->SMTPSecure = 'ssl';      // 加密方式  

25.     //$mail->SMTPAuth = true;         // 打开SMTP认证  

26.     //$mail->Username = "username";   // 用户名  

27.     //$mail->Password = "password";   // 密码  

28.   

29.     $mail->IsMail();                  // using PHP mail() function 有可能會出現這封郵件可能不是由以下使用者所傳送的提示  

30.               

31.     $mail->From = $sender;            // 发信人    

32.     $mail->FromName = $sender_name;   // 发信人别名    

33.     $mail->AddReplyTo($sender);       // 回覆人  

34.     $mail->AddAddress($receiver);     // 收信人    

35.   

36.     // 以html方式发送  

37.     if($ishtml){  

38.         $mail->IsHTML(true);  

39.     }  

40.   

41.     // 发送附件  

42.     if($attachments){  

43.         if(is_array($attachments)){  

44.             $send_attachments = array();  

45.   

46.             $tmp_attachments = array_slice($attachments,0,1);  

47.             if(!is_array(array_pop($tmp_attachments))){  

48.                 if(isset($attachments['path'])){  

49.                     array_push($send_attachments, $attachments);                      

50.                 }else{  

51.                     foreach($attachments as $attachment){  

52.                         array_push($send_attachments, array('path'=>$attachment));  

53.                     }  

54.                 }  

55.             }else{  

56.                 $send_attachments = $attachments;  

57.             }  

58.   

59.             foreach($send_attachments as $attachment){  

60.                 $attachment['name'] = isset($attachment['name'])? $attachment['name'] : null;  

61.                 $attachment['encoding'] = isset($attachment['encoding'])? $attachment['encoding'] : 'base64';  

62.                 $attachment['type'] = isset($attachment['type'])? $attachment['type'] : 'application/octet-stream';  

63.                 if(isset($attachment['path']) && file_exists($attachment['path'])){  

64.                     $mail->AddAttachment($attachment['path'],$attachment['name'],$attachment['encoding'],$attachment['type']);  

65.                 }  

66.             }  

67.         }elseif(is_string($attachments)){  

68.             if(file_exists($attachments)){  

69.                 $mail->AddAttachment($attachments);  

70.             }  

71.         }  

72.     }  

73.   

74.     $mail->Subject  = $subject;  // 邮件标题  

75.     $mail->Body     = $content;  // 邮件內容  

76.     return $mail->Send();    

77. }  

78.   

79. // DEMO  

80. $receiver = 'receiver@test.com';  

81. $sender = 'sender@test.com';  

82. $sender_name = 'sender name';  

83. $subject = 'subjecct';  

84. $content = 'content';  

85.   

86. // 四種格式都可以  

87. $attachments = 'attachment1.jpg';  

88. $attachments = array('path'=>'attachment1.jpg', 'name'=>'附件1.jpg');  

89. $attachments = array('attachment1.jpg','attachment2.jpg','attachment3.jpg');  

90. $attachments = array(  

91.     array('path'=>'attachment1.jpg', 'name'=>'附件1.jpg'),  

92.     array('path'=>'attachment2.jpg', 'name'=>'附件2.jpg'),  

93.     array('path'=>'attachment3.jpg', 'name'=>'附件3.jpg'),  

94. );  

95.   

96. $flag = sendMail($receiver, $sender, $sender_name, $subject, $content, true, $attachments);  

97. echo $flag;  

98.   

99. ?>  

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程