Yii2邮件程序compose()在循环中使用而不重置附件

时间:2017-03-11 07:06:24

标签: php yii2 phpmailer

我有以下代码,它将遍历$ byCreatedBy数组和 为每个$ createdBy创建xls文件,并为每个文件附加一个xls文件 $ createdBy。 但似乎

 $mail = \Yii::$app->mail->compose();

不会重新初始化$ attachment数组,从而导致下一条消息具有以前消息的附件/。

        foreach($byCreatedBy as $createdBy=>$data){
        $filename = \Yii::$app->params['data_dir']."pending-requisitions/pendingIR-".$createdBy.".xls";
        $this->xls = new MilPHPExcel;
        $this->writeRequisitions($data);
        $this->xls->outputFile($filename);
        $emailAddress = PeopleData::getEmailAddressByUsername($createdBy);

        $mail = \Yii::$app->mail->compose();
        $mail->setTo([
            'milwell@dexterton.loc' => 'Milwell',
        ])
        ->setSubject('Pending Internal Requistions')
        ->setHtmlBody(Yii::$app->view->render('/mail/pending-requisitions',[
            'username'=>$createdBy,
            'email'=>$emailAddress,
        ]))
        ->attach($filename)
        ->send();
        if($i>1)
            exit();
        $i++;
    }

1 个答案:

答案 0 :(得分:1)

将邮件代码更改为:

        $mail = \Yii::$app->mail;
        $mail->adapter->clearAttachments();
        $message = $mail->compose();
        $message->setTo([
            'milwell@dexterton.loc' => 'Milwell',
        ])
        ->setSubject('Pending Internal Requistions')
        ->setHtmlBody(Yii::$app->view->render('/mail/pending-requisitions',[
            'username'=>$createdBy,
            'email'=>$emailAddress,
        ]));
        $message->attach($filename);
        $message->send();

该行:

        $mail->adapter->clearAttachments();

将清除附件,因为我正在使用zyxphpmailer插件