Answer the question
In order to leave comments, you need to log in
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,
]);
}
}
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;
}
}
<?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(); ?>
Answer the question
In order to leave comments, you need to log in
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,
],
What's wrong? Try debugging the sending process
How to send emails via swiftMailer in Yii2?
'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 questionAsk a Question
731 491 924 answers to any question