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

时间:2015-04-25 07:02:22

标签: yii2 swiftmailer

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

此代码适用于多个收件人但无效。

$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();

如何在yii2邮件程序中添加setCc?

此代码用于添加setCc,但这也无效。

$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();

5 个答案:

答案 0 :(得分:12)

我刚刚尝试了以下代码,它正在运行。   代码中唯一奇怪的东西似乎是在带有数组的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();    

在Swift邮件代码中有以下注释:

 * Set the From address of this message.
 *
 * It is permissible for multiple From addresses to be set using an array.
 *
 * If multiple From addresses are used, you SHOULD set the Sender address and
 * according to RFC 2822, MUST set the sender address.

希望它有所帮助。

答案 1 :(得分:1)

为我工作:

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

答案 2 :(得分:1)

如何在 yii2 邮件程序中添加 setCc?

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

这是一种方法,将验证动态包含在控制器中,而不是通过模型验证,将输入框中的电子邮件地址列表转换为数组,无论输入框只有一项还是不止一项项。

在您的控制器中:

//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);
       }

}

答案 3 :(得分:0)

尝试解决方案:

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

答案 4 :(得分:0)

您只需要括在括号中:

['pedro@something.com', 'maria@something.com']

在类引用yii\swiftmailer\Message中有详细记录。

相关问题