Emailer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. //邮件发送集成
  3. namespace app\common\components;
  4. use app\models\EmailTpl;
  5. use app\models\EmailRecord;
  6. use app\models\EmailCert;
  7. use Yii;
  8. class Emailer
  9. {
  10. /*
  11. * $key:邮件模板key
  12. * $mailAddress:可以是单个邮箱地址,也可以是数组形式的多个邮箱地址,如:['222@mail.com','333@mail.com']
  13. * $args:模板参数示例:['name'=>'小知','code'=>'666666']
  14. * $attachments:附件
  15. * 代码示例: $mailer->send('sendcode',['171760754@qq.com','2629045294@qq.com'],array('code'=>222111,'brand'=>'知沃技术'),['https://www.test.com/upload/2022/05/22/16531799421b6dua.docx','https://www.test.com/upload/2022/05/22/1653179959jn0mu0.docx']);
  16. */
  17. public static function send($key,$mailAddress,$args=[],$attachments=[])
  18. {
  19. if(is_array($mailAddress)){
  20. $addressList = $mailAddress;
  21. }
  22. else
  23. {
  24. $addressList[] = $mailAddress;
  25. }
  26. //生成邮件内容
  27. $tpl = EmailTpl::find()->alias('a')->where("a.key='".$key."'")->one();
  28. $msgcontent = $tpl->tpl;
  29. if(is_array($args))foreach($args as $k=>$v)
  30. {
  31. $msgcontent = str_replace('${'.$k.'}',$v,$msgcontent);
  32. }
  33. //批量发送
  34. $messages = [];
  35. foreach ($addressList as $address) {
  36. $mail = Yii::$app->mailer->compose('default', [
  37. 'html' => 'html', //key固定,value是模版文件名
  38. 'title'=>$tpl->title,
  39. 'msgcontent' => $msgcontent,
  40. 'thbgcolor' => Yii::$app->params['mailer']['thbgcolor'],
  41. 'logo' => getFileUrl(Yii::$app->params['mailer']['logo']),
  42. 'fromtitle' => Yii::$app->params['mailer']['fromtitle'],
  43. 'team' => Yii::$app->params['mailer']['team'],
  44. 'app_url' => WEB_URL,
  45. ]);
  46. $mail->setTo($address);
  47. if(is_array($attachments))foreach($attachments as $attachment)
  48. {
  49. $mail->attach($attachment);
  50. }
  51. $mail->setSubject($tpl->title);
  52. $messages[] = $mail;
  53. //写邮件发送记录
  54. $record = new EmailRecord();
  55. $record->title = $tpl->title;
  56. $record->address = $address;
  57. $record->content = $msgcontent;
  58. $record->type = $tpl->type;
  59. $record->sent_time = TIMESTAMP;;
  60. $record->save();
  61. if($tpl->type==1)
  62. {
  63. $mobileCert = new EmailCert();
  64. $mobileCert->email = $address;
  65. $mobileCert->user_id = 0;
  66. $mobileCert->cert_key = strval($args['code']);
  67. $mobileCert->request_from = REQUEST_FROM;
  68. $mobileCert->sent_time = TIMESTAMP;
  69. $mobileCert->save();
  70. }
  71. }
  72. Yii::$app->mailer->sendMultiple($messages);
  73. return true;
  74. /* Yii::$app->mailer->compose('default', [
  75. 'html' => 'html', //key固定,value是模版文件名
  76. 'title'=>$tpl->title,
  77. 'msgcontent' => $msgcontent,
  78. 'thbgcolor' => Yii::$app->params['mailer']['thbgcolor'],
  79. 'logo' => getFileUrl(Yii::$app->params['mailer']['logo']),
  80. 'fromtitle' => Yii::$app->params['mailer']['fromtitle'],
  81. 'team' => Yii::$app->params['mailer']['team'],
  82. 'app_url' => WEB_URL,
  83. ])
  84. ->setTo('171760754@qq.com')
  85. ->setSubject($tpl->title)
  86. ->send();*/
  87. }
  88. }
  89. ?>