使用nette/mail封装一个发送邮件类(通用) 使用的包composerrequirenette/mail封装Mail体setFrom("{$host}","D88科技")->setHeader("name",$host);if(is_array($to)){foreach($toas$email){$this->addTo($email);}}}else{$this->addTo($to);}}publicfunctionfrom($from=null){if(!$from){thrownewInvalidArgumentException("邮件发送地址不能为空!");}$this->setFrom($from);返回$这个;}公共静态函数to($to=null){if(!$to){thrownewInvalidArgumentException("邮件接收地址不能为空!");}返回新邮件($to);}publicfunctiontitle($title=null){if(!$title){thrownewInvalidArgumentException("邮件标题不能为空!");}$this->setSubject($title);返回$这个;}publicfunctioncontent($content=null){if(!$content){thrownewInvalidArgumentException("邮件内容不能为空!");}$this->setHTMLBody($content);返回$这个;}}封装Mailer发送类title("ErrorWarning")->content($html);*Mailer::setMailer()->send($mail);**类邮件*@packageapp\common\server*/类邮件{/***实例化一个Mailer类*@returnSmtpMailer*/publicstaticfunctionsetMailer(){#这里的配置读取config配置$mailer=newSmtpMailer(['host'=>config('email.host'),'username'=>config('email.username'),'password'=>config('email.password'),'secure'=>config('email.secure')]);返回$邮件程序;}/***Send*@paramMessage$mail*/publicfunctionsend(Message$mail){$this->send($mail);}}配置'host'=>'smtp.exmail.qq.com',//使用qq的smtp服务器'username'=>'username','password'=>'password','secure'=>'ssl'//ssl是445端口,如果不设置,默认端口是22,见源代码。//$emails是一个数组//MailMessagebody$mail=Mail::to($emails)->title("ErrorWarning")->content($html);//SendMailer::setMailer()->发送($邮件);告诉我,如果你直接使用上面的方法,就是使用了同步发送机制。如果需要使用异步队列发送邮件,我提供如下解决方案。使用redis数据+key写入hashkeywrite进入list创建定时任务,去掉listkey,再去掉hash数据,发送理解,producerconsumer模式
