phpyiiyii2sendmailyii-components

How to send multiple settextbody yii2 send mail?


Hi I've trying to make a form that can send email using yii/mail, the problem is I only can send one form to become text body, I've code the models like this:

     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;

        }
    }

How can I use multiple parameters to

->setTextBody($this->body)

like

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

because in view I have several text input and radio list to send as an email, how can I do that?

My expectation on the text message will be like:

name
gender
variable 1
variable 2
variable n

SUMMARY edit = both answer is correct but I use

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

Thanks for Ankur Garg and Pratik Karmakar


Solution

  • good practice to put you html in separate file and prepare mail body in that file

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

    and the content _mail_body.php will be like this

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