Email.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\common\library;
  3. use think\Config;
  4. class Email
  5. {
  6. /**
  7. * 发送邮件
  8. * @param string $mTo 收件人
  9. * @param string $subject 邮件主题
  10. * @param string $content 邮件内容(html)
  11. * @param string $fromNic 发件人昵称
  12. * @param string $toNic 收件人昵称
  13. */
  14. public function sendMail($mTo='',$subject='',$content='',$fromNic='',$toNic='')
  15. {
  16. $site = Config::get("site");
  17. $re = Vendor('phpmailer.phpmailer.PHPMailerAutoload');
  18. $mail = new \PHPMailer ();
  19. //$mail->SMTPDebug = 3; // Enable verbose debug output
  20. $mail->isSMTP(); //smtp需要鉴权 这个必须是true
  21. $mail->Host = $site['mail_smtp_host']; //SMTP服务器地址
  22. $mail->SMTPAuth = true; // Enable SMTP authentication
  23. $mail->Username = $site['mail_smtp_user']; // SMTP 用戶名
  24. $mail->Password = $site['mail_smtp_pass']; // SMTP 密碼
  25. switch ($site['mail_verify_type']) // Enable TLS encryption, `ssl` also accepted
  26. {
  27. case 1:
  28. $mail->SMTPSecure = 'tls';
  29. break;
  30. case 2:
  31. $mail->SMTPSecure = 'ssl';
  32. break;
  33. default:
  34. $mail->SMTPSecure = '';
  35. }
  36. $mail->Port = $site['mail_smtp_port']; // 设置ssl连接smtp服务器的远程服务器端口号
  37. $mail->setFrom($site['mail_from'], $fromNic); // [发件人],[昵称(可选)]
  38. $mail->addAddress($mTo, $toNic); // [收件人],[昵称(可选)]
  39. //$mail->addReplyTo('xxxxxx@qq.com', 'Information'); // 回复地址(可选)
  40. //$mail->addCC('xxxxxx@qq.com'); //好像是密送
  41. //$mail->addBCC('xxxxxx@qq.com'); //好像是密送B
  42. // $mail->addAttachment('/var/tmp/file.tar.gz'); // 添加附件
  43. // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // 附件名选项
  44. $mail->isHTML(true); //邮件正文是否为html编码
  45. $mail->Subject = $subject; //添加邮件主题
  46. $mail->Body = $content; //邮件正文
  47. //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';//附加信息,可以省略
  48. switch ($site['mail_type'])
  49. {
  50. case 1:
  51. if(!$mail->send()) {//这里如果提交错误的smpt配置PHPmailer会卡住暂时不清楚为什么
  52. $sendResult['text'] = $mail->ErrorInfo;
  53. $sendResult['data'] = false;
  54. return $sendResult;
  55. } else {
  56. $sendResult['text'] ='smtp发送成功';
  57. $sendResult['data'] = true;
  58. return $sendResult;
  59. }
  60. break;
  61. case 2://使用mail方法发送邮件
  62. $headers = 'MIME-Version: 1.0' . "\r\n";
  63. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  64. $headers .= 'To: '.$toNic.' <'.$mTo.'>' . "\r\n";//收件人
  65. $headers .= 'From: '.$fromNic.' <'.$site['mail_from'].'>' . "\r\n";//发件人
  66. $sendResult['data'] = mail($mTo, $subject, $content, $headers);
  67. if ($sendResult['data']) {
  68. $sendResult['text'] ='mail函数发送成功';
  69. }else{
  70. $sendResult['text'] ='mail函数发送失败';
  71. }
  72. return $sendResult;
  73. break;
  74. default:
  75. $sendResult['data'] = false;
  76. $sendResult['text'] ='已关闭邮件发送';
  77. return $sendResult;
  78. }
  79. }
  80. }