S
S
Stepan2015-06-12 00:09:43
Yii
Stepan, 2015-06-12 00:09:43

Why is mail not being sent in yii2?

Controller

public function actionIndex(){
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        } else {
            return $this->render('contact', [
                'model' => $model,
            ]);
        }
    }

Model
public function contact($email)
    {
        if ($this->validate()) {
            Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom([$this->email => $this->name])
                ->setSubject($this->subject)
                ->setTextBody($this->body)
                ->send();

            return true;
        } else {
            return false;
        }
    }

The form
<?php $form = ActiveForm::begin(['id' => 'contact-form', 'options' => ['class' => 'contact ']]); ?>
                <?= $form->field($model, 'name')->label(false)->textInput(array('placeholder' => 'Представьтесь')) ?>
                <?= $form->field($model, 'email')->label(false)->textInput(array('placeholder' => 'Обратная связь (почта, skype, телефон)'))?>
                <?= $form->field($model, 'body')->textArea(['rows' => 6])->label(false)->textarea(array('placeholder' => 'Ваше сообщение'))?>
                <div class="form-group">
                    <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
                </div>
                <?php ActiveForm::end(); ?>

Mistake

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
NetyNicka, 2015-06-12
@xoma2

if the /mysuperapp/runtime folder contains your sent but not received emails, then you need to disable useFileTransport in the config file:

'mail' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
        ],

M
matperez, 2015-06-12
@matperez

What's wrong? Try debugging the sending process
How to send emails via swiftMailer in Yii2?

E
Evgeny Lavrentiev, 2015-06-12
@lavrentiev


'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@app/mail'
],
Disabling the transfer is an assumption, but what's the mistake in general? )))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question