如何发送多个settextbody yii2发送邮件?

时间:2017-09-27 02:33:50

标签: php yii yii2 sendmail yii-components

您好我正在尝试制作一个可以使用yii/mail发送电子邮件的表单,问题是我只能将一个表单发送到文本正文,我会对这些模型进行编码:

     class CareerForm extends Model

    {

        public $name;

        public $email;

        public $subject;

        public $body;

        public $verifyCode;

        public $gender;

        public function rules()

        {

            return [

            [['name', 'email', 'subject', 'body','gender'], 'required'],

            ['email', 'email'],

            ['verifyCode', 'captcha'],

            ];

         }
         public function career($email)

         {

            if ($this->validate()) {

            Yii::$app->mailer->compose()

                ->setTo($email)

                ->setFrom([$this->email => $this->name])

                ->setSubject($this->subject)

                ->setTextBody($this->body)

                ->send();



            return true;

        }

        return false;

        }
    }

如何使用多个参数

->setTextBody($this->body)

->setTextBody($this->body,$this->gender)

因为在视图中我有几个text inputradio list要作为电子邮件发送,我该怎么做?

我对短信的期望如下:

name
gender
variable 1
variable 2
variable n

内容 编辑=两个答案都是正确的,但我使用

public function career($email)
{
    if ($this->validate()) {
        Yii::$app->mailer->compose('filename.html' ,[
            'email' => $this->email,
            'name' => $this->name,
             ])
            ->setTo($email)
            ->setFrom([$this->email => $this->name])
            ->setSubject('career')
            ->send();

        return true;
    }
    return false;}

感谢Ankur Garg和Pratik Karmakar

2 个答案:

答案 0 :(得分:3)

将html放在单独的文件中并在该文件中准备邮件正文的好习惯

$body = $this->renderPartial('_mail_body.php' .[
                'gender' => $this->gender,
                'name' => $this->name,
    ]);

,内容_mail_body.php将是这样的

<html>
<body>
<table cellpadding="0" cellspacing="0" align="center"  width="672px" style="font-size:24px; text-align:center;">
<tr>
    <td width="670px" align="center" style="border-left:1px solid #e0e0e0; border-right:1px solid #e0e0e0; font-size:24px; font-family:Arial, Helvetica, sans-serif; padding-top:47px;">
        <table width="608px" cellpadding="0" cellspacing="0" align="center">
            <tr>
                <td width="178px" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; border-right:1px solid #e0e0e0; padding:11px;">
                    Name
                </td>
                <td width="427px" style="font-family:Arial, Helvetica, sans-serif; font-size:14px;padding:11px; color:#4e4e4e;">
                    <?php echo $name;?>
                </td>
            </tr>
            <tr>
                <td width="178px" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; border-right:1px solid #e0e0e0; padding:11px;">
                    Gender
                </td>
                <td width="427px" style="font-family:Arial, Helvetica, sans-serif; font-size:14px;padding:11px; color:#4e4e4e;">
                    <?php echo $gender;?>
                </td>
            </tr>
        </table>
    </td>
</tr>
</table>
</body>
</html>

答案 1 :(得分:1)

传递多个参数的最佳方法是在邮件目录中创建另一个视图文件并传递参数。

    public function career($email)
    {
      $message = Yii::$app->mailer->compose("file_name", ['body' => $this->body,'gender'=>$this->gender])
                    ->setTo($email)
                    ->setFrom([$this->email => $this->name])
                    ->setSubject($this->subject)
                    ->setTextBody($this->body)
                    ->send();

    } 

注意:在邮件目录中创建文件“file_name”。