SendBxMail.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\common\command;
  3. use app\adminapi\lists\works\ServiceWorkMailLists;
  4. use excel\ExcelWriter;
  5. use PHPMailer\PHPMailer\PHPMailer;
  6. use think\console\Command;
  7. use think\console\Input;
  8. use think\console\Output;
  9. use think\facade\Log;
  10. /**
  11. * 发送保险的邮箱
  12. * Class SendBxMail
  13. * @package app\command
  14. */
  15. class SendBxMail extends Command
  16. {
  17. protected function configure()
  18. {
  19. $this->setName('send_bx_mail')->setDescription('每天22点定时发送邮箱');
  20. }
  21. protected function execute(Input $input, Output $output)
  22. {
  23. try {
  24. return $this->sendMail();
  25. } catch (\Exception $e) {
  26. Log::write('SendBxMail:'.$e->getMessage());
  27. return false;
  28. }
  29. }
  30. public function sendMail()
  31. {
  32. // 文件路径 - 替换为你的实际文件路径
  33. //$filePath = 'uploads/baoxian/20250327职业责任险投保清单.xlsx';
  34. $excelFile = $this->getExcelFile();
  35. $filePath = $excelFile[0];
  36. $filename = $excelFile[1];
  37. if(empty($filePath)) throw new \Exception("邮件发送失败. 错误信息: 路径不存在");
  38. /*$mail = new PHPMailer(true);
  39. try {
  40. // 邮件服务器设置
  41. $mail->isSMTP(); // 使用SMTP发送邮件
  42. $mail->Host = 'smtp.126.com'; // SMTP服务器地址,替换为你的SMTP服务器
  43. $mail->SMTPAuth = true; // 启用SMTP认证
  44. $mail->Username = 'whkyjl@126.com'; // SMTP用户名,替换为你的邮箱地址
  45. $mail->Password = 'MNpgMjYMYehx3nPi'; // SMTP密码,替换为你的邮箱密码
  46. $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // 开启加密,默认为无加密,可选`PHPMailer::ENCRYPTION_SMTPS`
  47. $mail->Port = 465;
  48. // $mail->SMTPDebug = 2; // 启用详细调试输出
  49. $mail->CharSet = 'UTF-8';
  50. // 发送者和接收者设置
  51. $mail->setFrom('whkyjl@126.com', '武汉开源节流科技有限公司'); // 发件人邮箱和名称
  52. $mail->addAddress('sujing@ub.chinalife-p.com.cn', '出单业务'); // 收件人邮箱和名称
  53. $mail->addAddress('649478907@qq.com', '出单业务'); // 收件人邮箱和名称
  54. $mail->addAddress('liliangjie@ub.chinalife-p.com.cn', '出单业务'); // 收件人邮箱和名称
  55. $mail->addAddress('fangxuhao@outlook.com', '出单业务'); // 收件人邮箱和名称
  56. // $mail->addAddress('1804628603@qq.com', '出单业务'); // 收件人邮箱和名称
  57. // 设置邮件内容
  58. $mail->isHTML(true); // 将邮件正文设置为HTML格式
  59. // 设置邮件内容
  60. $mail->Subject = mb_convert_encoding('请查收附件中的Excel文件', 'UTF-8', 'auto');
  61. $mail->Body = mb_convert_encoding('<p>你好,<br>请查收附件中的Excel文件。</p>', 'UTF-8', 'auto');
  62. $mail->AltBody = mb_convert_encoding('你好,请查收附件中的Excel文件。', 'UTF-8', 'auto');
  63. // 添加附件
  64. $mail->addAttachment($filePath, $filename); // 添加附件,第二个参数是显示在邮件中的文件名
  65. $mail->send();
  66. return '邮件发送成功';
  67. } catch (\Exception $e) {
  68. throw new \Exception("邮件发送失败. 错误信息: {$mail->ErrorInfo}");
  69. }*/
  70. }
  71. public function getExcelFile()
  72. {
  73. try {
  74. $downloadObj = new ServiceWorkMailLists();
  75. $sheet = new ExcelWriter();
  76. $lists = $downloadObj->excelExportList([]);
  77. $filename = $downloadObj->setFileName().'职业责任险投保清单';
  78. $fields = $downloadObj->setExcelComplexFields();
  79. if(!$lists || !$fields){
  80. Log::info('ServiceWorkMail:lists或fields不存在');
  81. return '';
  82. }else{
  83. $sheet->savePath = './public/exports/';
  84. $sheet->generateExcelFile($fields['zh_cn_fields'], $lists,$filename, $fields['data_fields']);
  85. return [$sheet->fileUrl(),$filename];
  86. }
  87. } catch (\Exception $e) {
  88. dd($e);
  89. Log::info('ServiceWorkMail:'."生成职业责任险投保清单失败. 错误信息: {$e->getMessage()}");
  90. return '';
  91. }
  92. }
  93. }