Mail.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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');
  11. $mail->setFrom(SettingModel::Config('title', '') . " <$send_mail>")
  12. ->addTo($to)
  13. ->setSubject(SettingModel::Config('title', '') . '动态令牌')
  14. ->setHtmlBody($text);
  15. $option = [
  16. 'port' => SettingModel::Config('smtp_port'),
  17. 'host' => SettingModel::Config('smtp_host'),
  18. 'username' => SettingModel::Config('smtp_email'),
  19. 'password' => SettingModel::Config('smtp_password'),
  20. ];
  21. if ((int)$option['port'] === 465) {
  22. $option['secure'] = 'ssl';
  23. }
  24. $mailer = new SmtpMailer($option);
  25. $mailer->send($mail);
  26. return true;
  27. }
  28. public static function testMail($to, $config): bool
  29. {
  30. $mail = new Message;
  31. $send_mail = $config['smtp_email'];
  32. $mail->setFrom("测试邮件 <$send_mail>")
  33. ->addTo($to)
  34. ->setSubject('测试邮件')
  35. ->setHtmlBody("这是一个测试邮件");
  36. $option = [
  37. 'port' => $config['smtp_port'],
  38. 'host' => $config['smtp_host'],
  39. 'username' => $config['smtp_email'],
  40. 'password' => $config['smtp_password'],
  41. ];
  42. if ((int)$option['port'] === 465) {
  43. $option['secure'] = 'ssl';
  44. }
  45. $mailer = new SmtpMailer($option);
  46. $mailer->send($mail);
  47. return true;
  48. }
  49. }