'小知','code'=>'666666'] * $attachments:附件 * 代码示例: $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']); */ public static function send($key,$mailAddress,$args=[],$attachments=[]) { if(is_array($mailAddress)){ $addressList = $mailAddress; } else { $addressList[] = $mailAddress; } //生成邮件内容 $tpl = EmailTpl::find()->alias('a')->where("a.key='".$key."'")->one(); $msgcontent = $tpl->tpl; if(is_array($args))foreach($args as $k=>$v) { $msgcontent = str_replace('${'.$k.'}',$v,$msgcontent); } //批量发送 $messages = []; foreach ($addressList as $address) { $mail = Yii::$app->mailer->compose('default', [ 'html' => 'html', //key固定,value是模版文件名 'title'=>$tpl->title, 'msgcontent' => $msgcontent, 'thbgcolor' => Yii::$app->params['mailer']['thbgcolor'], 'logo' => getFileUrl(Yii::$app->params['mailer']['logo']), 'fromtitle' => Yii::$app->params['mailer']['fromtitle'], 'team' => Yii::$app->params['mailer']['team'], 'app_url' => WEB_URL, ]); $mail->setTo($address); if(is_array($attachments))foreach($attachments as $attachment) { $mail->attach($attachment); } $mail->setSubject($tpl->title); $messages[] = $mail; //写邮件发送记录 $record = new EmailRecord(); $record->title = $tpl->title; $record->address = $address; $record->content = $msgcontent; $record->type = $tpl->type; $record->sent_time = TIMESTAMP;; $record->save(); if($tpl->type==1) { $mobileCert = new EmailCert(); $mobileCert->email = $address; $mobileCert->user_id = 0; $mobileCert->cert_key = strval($args['code']); $mobileCert->request_from = REQUEST_FROM; $mobileCert->sent_time = TIMESTAMP; $mobileCert->save(); } } Yii::$app->mailer->sendMultiple($messages); return true; /* Yii::$app->mailer->compose('default', [ 'html' => 'html', //key固定,value是模版文件名 'title'=>$tpl->title, 'msgcontent' => $msgcontent, 'thbgcolor' => Yii::$app->params['mailer']['thbgcolor'], 'logo' => getFileUrl(Yii::$app->params['mailer']['logo']), 'fromtitle' => Yii::$app->params['mailer']['fromtitle'], 'team' => Yii::$app->params['mailer']['team'], 'app_url' => WEB_URL, ]) ->setTo('171760754@qq.com') ->setSubject($tpl->title) ->send();*/ } } ?>