123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace app\common\components;
- use app\models\EmailTpl;
- use app\models\EmailRecord;
- use app\models\EmailCert;
- use Yii;
- class Emailer
- {
-
- 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',
- '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;
-
- }
- }
- ?>
|