alert("开始发送邮件"); $config = Config('site'); if(isset($data['config'])){ $config = $data['config']; unset($data['config']); } try { $mail = new \Nette\Mail\Message; $mailer = new \Nette\Mail\SmtpMailer( host: $config['mail_smtp_host'], username: $config['mail_smtp_user'], password: $config['mail_smtp_pass'], encryption: \Nette\Mail\SmtpMailer::EncryptionSSL, ); $mail->setFrom($config['mail_from']) ->addTo($data['email']) ->setSubject($data['title']); if($data['body']){ $mail->setHtmlBody($data['body']); }else{ $mail->setHtmlBody($this->getTemplate($data)); } $mailer->send($mail); \support\Log::channel('mail')->alert($data['email']."邮件已经发送"); } catch (\Throwable $th) { \support\Log::channel('mail')->alert('发送邮件出错1:'.$th->getMessage()); \support\Log::channel('mail')->alert(json_encode($data)); } // 无需反序列化 //var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello'] } function getTemplate($data){ $config = Config('site'); $mail_from = $config['mail_from']; $str = '
Thanks for your request!
Do not share this code with anyone, even if they claim to be a Codify employee.
{$code}
Valid within 15 minutes and
can only be used once.
Need help with anything? connect support
'; foreach($data as $k=>$v){ $str = str_replace('{$'.$k.'}',$v,$str); } return $str ; } // 消费失败回调 /* $package = [ 'id' => 1357277951, // 消息ID 'time' => 1709170510, // 消息时间 'delay' => 0, // 延迟时间 'attempts' => 2, // 消费次数 'queue' => 'send-mail', // 队列名 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 'max_attempts' => 5, // 最大重试次数 'error' => '错误信息' // 错误信息 ] */ public function onConsumeFailure(\Throwable $e, $package) { echo "consume failure\n"; echo $e->getMessage() . "\n"; // 无需反序列化 //var_export($package); } }