MailHelper.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/9/26
  12. */
  13. namespace lemo\helper;
  14. class MailHelper{
  15. /**
  16. * 邮件发送
  17. * @param $to 接收人
  18. * @param string $subject 邮件标题
  19. * @param string $content 邮件内容(html模板渲染后的内容)
  20. * @throws Exception
  21. * @throws phpmailerException
  22. */
  23. public static function sendEmail($to,$subject='',$content=''){
  24. vendor('phpmailer.PHPMailerAutoload'); ////require_once vendor/phpmailer/PHPMailerAutoload.php';
  25. //判断openssl是否开启
  26. $openssl_funcs = get_extension_funcs('openssl');
  27. if(!$openssl_funcs){
  28. return array('status'=>-1 , 'msg'=>'请先开启openssl扩展');
  29. }
  30. $mail = new PHPMailer;
  31. $config = tpCache('smtp');
  32. $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
  33. $mail->isSMTP();
  34. //Enable SMTP debugging
  35. // 0 = off (for production use)
  36. // 1 = client messages
  37. // 2 = client and server messages
  38. $mail->SMTPDebug = 0;
  39. //调试输出格式
  40. //$mail->Debugoutput = 'html';
  41. //smtp服务器
  42. $mail->Host = $config['smtp_server'];
  43. //端口 - likely to be 25, 465 or 587
  44. $mail->Port = $config['smtp_port'];
  45. if($mail->Port == 465) $mail->SMTPSecure = 'ssl';// 使用安全协议
  46. //Whether to use SMTP authentication
  47. $mail->SMTPAuth = true;
  48. //用户名
  49. $mail->Username = $config['smtp_user'];
  50. //密码
  51. $mail->Password = $config['smtp_pwd'];
  52. //Set who the message is to be sent from
  53. $mail->setFrom($config['smtp_user']);
  54. //回复地址
  55. //$mail->addReplyTo('replyto@example.com', 'First Last');
  56. //接收邮件方
  57. if(is_array($to)){
  58. foreach ($to as $v){
  59. $mail->addAddress($v);
  60. }
  61. }else{
  62. $mail->addAddress($to);
  63. }
  64. $mail->isHTML(true);// send as HTML
  65. //标题
  66. $mail->Subject = $subject;
  67. //HTML内容转换
  68. $mail->msgHTML($content);
  69. //Replace the plain text body with one created manually
  70. //$mail->AltBody = 'This is a plain-text message body';
  71. //添加附件
  72. //$mail->addAttachment('images/phpmailer_mini.png');
  73. //send the message, check for errors
  74. if (!$mail->send()) {
  75. return array('status'=>-1 , 'msg'=>'发送失败: '.$mail->ErrorInfo);
  76. } else {
  77. return array('status'=>1 , 'msg'=>'发送成功');
  78. }
  79. }
  80. }