如何在Yii2 mailer中向多个收件人发送邮件或如何在Yii2 mailer中添加setCc

如何在Yii2 mailer中向多个收件人发送邮件或如何在Yii2 mailer中添加setCc,yii2,swiftmailer,Yii2,Swiftmailer,如何在Yii2 mailer中向多个收件人发送邮件? 此代码适用于多个收件人,但不起作用 $value = Yii::$app->mailer->compose() ->setFrom([$this->email => $this->name]) ->setTo(array($model->email_1,$model->email_2)) ->setSubject

如何在Yii2 mailer中向多个收件人发送邮件?

此代码适用于多个收件人,但不起作用

$value = Yii::$app->mailer->compose()
            ->setFrom([$this->email => $this->name])
            ->setTo(array($model->email_1,$model->email_2))
            ->setSubject($model->status)
            ->setHtmlBody($model->description)
            ->send();
$value = Yii::$app->mailer->compose()
            ->setFrom([$this->email => $this->name])
            ->setTo($model->email_1)
            ->setCc($model->email_2)
            ->setSubject($model->status)
            ->setHtmlBody($model->description)
            ->send();

如何在yii2邮件中添加setCc?

此代码用于添加setCc,但也不起作用

$value = Yii::$app->mailer->compose()
            ->setFrom([$this->email => $this->name])
            ->setTo(array($model->email_1,$model->email_2))
            ->setSubject($model->status)
            ->setHtmlBody($model->description)
            ->send();
$value = Yii::$app->mailer->compose()
            ->setFrom([$this->email => $this->name])
            ->setTo($model->email_1)
            ->setCc($model->email_2)
            ->setSubject($model->status)
            ->setHtmlBody($model->description)
            ->send();

我刚刚尝试了下面的代码,它正在工作。 代码中唯一奇怪的地方似乎是带有数组的setFrom

        Yii::$app->mailer->compose()
            ->setFrom('addrs1@gmail.com')
            ->setTo(array('addrs1@gmail.com', 'addrs2@hotmail.com'))
            ->setCc(array('addrs3@gmail.com'))
            ->setSubject('Sending Email')
            ->setTextBody('This is Plain text content')
            ->setHtmlBody('Please go to  <a href="http://google.com/">GOOGLE</a>')
            ->send();    
希望有帮助。

尝试解决方案:

$mail = Yii::$app->mailer->compose($mail_type, $params)
        ->setFrom([ self::$_sender => self::$_senderName ])
        ->setSubject($subject);
    foreach(self::$_to as $receiver){
        $mail->setTo($receiver)
            ->send();
    }

您只需将以下内容括在括号内:

['pedro@something.com', 'maria@something.com']
它在课堂参考资料中有很好的记录。

对我很有用:

            ->setTo([
                    'john.doe@gmail.com' => 'John Doe',
                    'jane.doe@gmail.com' => 'Jane Doe',
            ])

如何在yii2邮件中添加setCc?

上述答案的不足之处,尤其是对于新手来说,在于没有提到如何将视图中inputbox中的值转换为上述数组。值得注意的是,输入框中由逗号分隔的电子邮件地址列表不是数组,而是列表。单个值不是一个列表。那么,我们如何捕获可能的单个值和多个值:

这里有一种方法,将验证动态地包含在控制器中,而不是通过模型进行验证,以将inputbox中的电子邮件地址列表转换为一个数组,无论inputbox仅包含一个项目还是多个项目

在控制器中:

//You can use the EmailValidator to validate each
//individual post from the view. In my case my 
//cc inputbox has a name = "cc" and id = "cc"  
use yii\validators\EmailValidator;

//Assuming your view stresses separation of fields 
//with ONLY a comma ..as tip to inputbox.

public function emailinvoice() {

$validator = new EmailValidator([
            'enableIDN'=>true,
            'checkDNS'=>true
]);

//if getting value from view directly WITHOUT a model: id and name of inputbox is 'cc'
$cc = Yii::$app->request->post('cc');

//if getting value from model: field is called 'email_2'
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$cc = $model->email_2;  

//catch multiple addresses by building an array
//as mentioned above 
$array = [];
//the field is not empty => single address, or  there is a separator comma in it => multiple addresses.

//You can add more validators here ie || (strpos($cc, ';')   
if (!empty($cc)||(strpos($cc, ','))){
             //remove comma
             $cc = explode(',', $cc);
             //after explode cc is an array. 
             //so $cc[0] = 'name@domain.com'
             //and $cc[1] = ' name2@domain.com'
             //Note the space before the second 
             //element which you will have to remove.  
             //Each array component could have a 
             //space before it especially the second 
             //element so trim spaces for all items 
             //in new array
             $i = 0;
             foreach ($cc as $address) {
                //remove the potential spaces
                $address = ltrim(rtrim($address));
                if ($validator->validate($address)) {
                    //include in new array
                    $array[$i] = $address; 
                }
                else {
                    Yii::$app->session->setFlash('danger', 'cc error'.$i);
                }
                $i+=1;
             }
             $send->setCc($array);
       }

}

什么错误提醒您,发生了什么?