Swiftmailer in Yii2 - Unable to send email

╄→гoц情女王★ 提交于 2020-01-06 02:43:28

问题


I'm using basic Yii2 template and Swiftmailer to send email..

Here is my code for config/web.php:

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport'=>'false',
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => 'myemail@gmail.com',
                'password' => 'password',
                'port' => '587',
                'encryption' => 'tls',
            ],
        ],

and my controller code is:

$fname = Yii::$app->request->post('fname');
        $email = Yii::$app->request->post('email');

        $industry = Yii::$app->request->post('industry');
        $info = Yii::$app->request->post('info');

        $mail = Yii::$app->mailer->compose()
            ->setFrom('myemail@gmail.com')
            ->setTo($email)
            ->setSubject('Test mail')
            ->setTextBody($info)
            ->send();

I'm getting email logs, but no email is being send...

Thanks.


回答1:


In order to keep your account less vulnerable, google has introduced this policy. As we login through our account without creating any app on Google, Google ask for our permission to ensure the security of our account.

Access below url to fix this problem and select "Turn on" https://www.google.com/settings/security/lesssecureapps




回答2:


Attention you assign the result of the email compose()...->send() to $mail in this way you don't send the email but store the result in this variable. try without assigment like:

    $industry = Yii::$app->request->post('industry');
    $info = Yii::$app->request->post('info');

    Yii::$app->mailer->compose()
        ->setFrom('myemail@gmail.com')
        ->setTo($email)
        ->setSubject('Test mail')
        ->setTextBody($info)
        ->send();

Also attention to the

'useFileTransport'=>'false',

false i PHP value, I think you need use

 'useFileTransport'=>false,

whitout the ' character




回答3:


I got this same problem and I used port=>25 which solve my problem.




回答4:


scaisEdge is right:

You set 'useFileTransport'=>'false'. Since 'false' is a string, this will evaluate to (bool) true. You need to set it to 'useFileTransport' => false.

Further:

If you're using something like AWS beanstalk environment variables which are always set as string, you want to evaluate it to true like this:

'useFileTransport' => $_ENV['APP_STOPMAILER'] === "true" ? true : false,



来源:https://stackoverflow.com/questions/30232720/swiftmailer-in-yii2-unable-to-send-email

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!