S
S
Svetlana Galenko2018-11-14 20:54:26
Yii
Svetlana Galenko, 2018-11-14 20:54:26

Why are letters not sent from the yii2 site to the mail?

Hello, emails are not sent from the yii2 site to the mail. I am using Phpmailer.
Here's what I wrote in the config:

'mailer' => [
            'class'            => 'zyx\phpmailer\Mailer',
            'viewPath'         => '@app/mail',
            'useFileTransport' => false,
            'config'           => [
                'mailer'     => 'smtp',
                'host'       => 'smtp.yandex.ru',
                'port'       => '587',
                'smtpsecure' => 'tls',
                'smtpauth'   => true,
                'username'   => '[email protected]',
                'password'   => 'test',
            ],
        ],

in Controller:
public function actionIndex()
    {
        /* Создаем экземпляр класса */
        $model = new AppointmentForm();
        /* получаем данные из формы и запускаем функцию отправки contact, если все хорошо, выводим сообщение об удачной отправке сообщения на почту */
        if ($model->load(Yii::$app->request->post()) && $model->appointment(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');
            return $this->refresh();
            /* иначе выводим форму обратной связи */
        }  else {
            return $this->render('index', ['model'=> $model,]);
        }
    }

In the module:
public function appointment($email)
    {
        $content = "<p>Email: " . $this->email . "</p>";
        $content .= "<p>Name: " . $this->name . "</p>";
        $content .= "<p>Phone: " . $this->phone . "</p>";
        $content .= "<p>Date: " . $this->date . "</p>";
        $content .= "<p>Body: " . $this->body . "</p>";
        if ($this->validate()) {
            Yii::$app->mailer->compose(["content" => $content])
                ->setTo('[email protected]')
                ->setFrom([\Yii::$app->params['supportEmail'] => $this->name])
                ->setTextBody($this->body)
                ->send();

            return true;
        }
        return false;
    }

In params.php
return [
    'adminEmail' => '[email protected]',
    'supportEmail' => '[email protected]', //автоматическая отправка почты с данного сайта
];

Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question