Mailing - Contact Form

Hello,

I have a question regarding contact form and collecting the user input and send it to an email. I’m using Yii2. I was working off of the the default contact form but when sending the information I just see the <body> content of the form, but not the other fields like first name, email zip, etc. Can you help with on how-to?

/Model/


    public function contact($email)

    {

        if ($this->validate()) {

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

                ->setTo(['shabangu@msoe.edu'])

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

                ->setSubject($this->subject)

                ->setTextBody($this->firstName)

                ->setTextBody($this->lastName)

                ->setTextBody($this->jenzId)

                ->setTextBody($this->email)

                ->setTextBody($this->subject)

                ->setTextBody($this->body)

                ->send();

 

            return true;

        }

        return false;

    }

Here’s View


<?= $form->field($model, 'firstName')->textInput(['autofocus' => true]) ?></div>

                    <div class="span6"><?= $form->field($model, 'lastName')->textInput(['autofocus' => true]) ?></div></div>

                    <?= $form->field($model, 'jenzId') ?>

                    <?= $form->field($model, 'email') ?>

                   

                    <?= $form->field($model, 'city')->textInput(); ?>

                    <?= $form->field($model, 'state')->textInput(); ?>

                    <!--this is type tel so it propmts the number pad on mobile devices.-->

                    <?= $form->field($model, 'zip')->input('tel'); ?>

                    <?= $form->field($model, 'country')->textInput(); ?>

                    <?= $form->field($model, 'subject')->textInput(); ?>

                    <?= $form->field($model, 'body')->textarea(['rows' => 6]) ?>

You are calling setTextBody 6 times with different parts, but each call is overwriting the previous content and you wii get only the last one.

Concatinate all the parts and call setTextBody only one time.

to add to @softark’s answer here is an example


$body = $this->firstName;

$body .= $this->lastName;

$body .= $this->jenzId;

$body .= $this->email;

$body .= $this->subject;

$body .= $this->body;


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

    ->setTo(['shabangu@msoe.edu'])

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

    ->setSubject($this->subject)

    ->setTextBody($body)

    ->send();