【问题标题】:How to send mail to multiple recipients in Yii2 mailer OR how to add setCc in yii2 mailer如何在 Yii2 邮件程序中向多个收件人发送邮件或如何在 yii2 邮件程序中添加 setCc
【发布时间】:2015-04-25 07:02:21
【问题描述】:

如何在 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();

yii2 mailer如何添加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();

【问题讨论】:

  • 什么错误提醒你,会发生什么?

标签: yii2 swiftmailer


【解决方案1】:

我刚刚尝试了以下代码,它正在工作。 您的代码中唯一奇怪的地方似乎是在带有数组的 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 邮件程序代码中有以下 cmets:

 * 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.

希望对你有帮助。

【讨论】:

    【解决方案2】:

    为我工作:

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

    【讨论】:

      【解决方案3】:

      yii2 mailer如何添加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);
             }
      

      }

      【讨论】:

        【解决方案4】:

        尝试解决方案:

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

        【讨论】:

          【解决方案5】:

          只需要用括号括起来:

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

          在类参考yii\swiftmailer\Message中有详细记录。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-30
            • 2015-10-21
            • 1970-01-01
            相关资源
            最近更新 更多