Email.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php namespace Msg\Lib\Method;
  2. use Dever;
  3. class Email
  4. {
  5. private $config;
  6. # 初始化
  7. public function init($template_id, $account)
  8. {
  9. if (!Dever::project('email')) {
  10. Dever::error('账户不存在');
  11. }
  12. if (empty($account['email'])) {
  13. Dever::error('账户不存在');
  14. }
  15. $this->config = Dever::db('template_email', 'msg')->find(array('template_id' => $template_id));
  16. if (!$this->config) {
  17. Dever::error('邮件配置不存在');
  18. }
  19. $this->config['account'] = $account['email'];
  20. return $this->config['account'];
  21. }
  22. # 发送短信
  23. public function send($content, $param)
  24. {
  25. if (!$this->config) {
  26. Dever::error('短信配置不存在');
  27. }
  28. Dever::apply('PHPMailer', 'email', 'src');
  29. Dever::apply('Exception', 'email', 'src');
  30. Dever::apply('SMTP', 'email', 'src');
  31. $mail = new \PHPMailer\PHPMailer\PHPMailer();
  32. list($host, $port) = explode(':', $this->config['host']);
  33. $mail->isSMTP();
  34. //$mail->SMTPDebug = 2;
  35. $mail->CharSet = 'UTF-8';
  36. $mail->Host = $host;
  37. $mail->Port = $port;
  38. if ($port == 465) {
  39. $mail->SMTPSecure = 'ssl';
  40. } else {
  41. $mail->SMTPSecure = 'tls';
  42. }
  43. $mail->SMTPAuth = true;
  44. $mail->Username = $this->config['user'];
  45. $mail->Password = $this->config['pwd'];
  46. $mail->setFrom($this->config['user'], $this->config['username']);
  47. $mail->addAddress($this->config['account'], $this->config['account']);
  48. if (empty($param['title'])) {
  49. $param['title'] = $this->config['title'];
  50. }
  51. $mail->Subject = "=?utf-8?B?" . base64_encode($param['title']) . "?=";
  52. $mail->Body = $content;
  53. $mail->isHTML(true);
  54. if (isset($param['file'])) {
  55. $mail->addAttachment($param['file']);
  56. }
  57. if (!$mail->send()) {
  58. $param['error'] = $mail->ErrorInfo;
  59. Dever::log($param, 'email');
  60. Dever::error("Mailer Error: " . $mail->ErrorInfo);
  61. } else {
  62. return 'ok';
  63. }
  64. }
  65. }