1

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

Elydasian
  • 2,016
  • 5
  • 23
  • 41
unknown.person
  • 135
  • 2
  • 11

2 Answers2

3

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>
Ankur Garg
  • 577
  • 3
  • 9
  • thanks for the answer, but i didn't use renderpartial, instead i used ->compose("file_name", ['body' => $this->body,'gender'=>$this->gender],...), thanks for helping me – unknown.person Sep 27 '17 at 07:41
1

The Best Way to Pass multiple parameters is to create a another view file inside the mail directory and pass the Parameters.

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

    } 

Note: Create the file 'file_name' inside the mail directory.

Pratik Karmakar
  • 247
  • 1
  • 8
  • the settextbody still using ->setTextBody($this->body) ? or the parameters should be change with "file_name"? – unknown.person Sep 27 '17 at 04:55
  • No, You can remove the option setTextBody as it is not required now. Pass all the parameters to the "file_name" as you do at the time of rendering a file – Pratik Karmakar Sep 27 '17 at 06:54
  • 1
    thanks, this is also correct answer, but the other answer give an "file_name" example, but still this is correct answer so i vote up, thank you – unknown.person Sep 27 '17 at 07:39