123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- //邮件发送集成
- namespace app\common\components;
- use app\models\EmailTpl;
- use app\models\EmailRecord;
- use app\models\EmailCert;
- use Yii;
- class Emailer
- {
- /*
- * $key:邮件模板key
- * $mailAddress:可以是单个邮箱地址,也可以是数组形式的多个邮箱地址,如:['222@mail.com','333@mail.com']
- * $args:模板参数示例:['name'=>'小知','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();*/
- }
- }
- ?>
|