Mail.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. use Nette\Mail\Message;
  3. use Nette\Mail\SmtpMailer;
  4. use \app\model\SettingModel;
  5. class Mail
  6. {
  7. public static function send($to = "", $text = ""): bool
  8. {
  9. $mail = new Message;
  10. $send_mail = SettingModel::Config('smtp_email',false);
  11. $option = [
  12. 'port' => SettingModel::Config('smtp_port'),
  13. 'host' => SettingModel::Config('smtp_host',false),
  14. 'username' => SettingModel::Config('smtp_email'),
  15. 'password' => SettingModel::Config('smtp_password'),
  16. ];
  17. if (!$send_mail || !$option['host']) {
  18. return abort(0, "管理员没有配置SMTP邮件服务");
  19. }
  20. $mail->setFrom(SettingModel::Config('title', '') . " <$send_mail>")
  21. ->addTo($to)
  22. ->setSubject(SettingModel::Config('title', '') . '动态令牌')
  23. ->setHtmlBody($text);
  24. if ((int)$option['port'] === 465) {
  25. $option['secure'] = 'ssl';
  26. }
  27. $mailer = new SmtpMailer($option);
  28. $mailer->send($mail);
  29. return true;
  30. }
  31. public static function testMail($to, $config): bool
  32. {
  33. $mail = new Message;
  34. $send_mail = $config['smtp_email'];
  35. $mail->setFrom("测试邮件 <$send_mail>")
  36. ->addTo($to)
  37. ->setSubject('测试邮件')
  38. ->setHtmlBody("这是一个测试邮件");
  39. $option = [
  40. 'port' => $config['smtp_port'],
  41. 'host' => $config['smtp_host'],
  42. 'username' => $config['smtp_email'],
  43. 'password' => $config['smtp_password'],
  44. ];
  45. if ((int)$option['port'] === 465) {
  46. $option['secure'] = 'ssl';
  47. }
  48. $mailer = new SmtpMailer($option);
  49. $mailer->send($mail);
  50. return true;
  51. }
  52. }