MailService.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Mail;
  4. use App\Mail\InquiryMail;
  5. class MailService
  6. {
  7. /**
  8. * 发送询盘邮件(静态方法)
  9. *
  10. * @param array $inquiryData 询盘数据
  11. * @return bool 返回是否发送成功
  12. */
  13. public static function sendInquiryMail(array $inquiryData)
  14. {
  15. try {
  16. $toAddress = config('mail_recipients.to.address');
  17. $ccAddress = config('mail_recipients.cc.address');
  18. if (empty($toAddress)) {
  19. // 没有收件人地址时,记录日志并返回 false
  20. logger()->warning('邮件未发送:缺少收件人地址');
  21. return false;
  22. }
  23. $mail = Mail::to($toAddress);
  24. if (!empty($ccAddress)) {
  25. $mail->cc($ccAddress);
  26. }
  27. $mail->send(new InquiryMail($inquiryData));
  28. return true; // 成功发送邮件
  29. } catch (\Exception $e) {
  30. // 处理邮件发送失败的异常
  31. logger()->error('邮件发送失败: ' . $e->getMessage());
  32. return false;
  33. }
  34. }
  35. }