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

How to send multiple settextbody yii2 send mail?

你好我正在尝试制作一个可以使用 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

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

    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'。

将您 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>