Email.php 4.0 KB

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