Yii2 SwiftMailer sending email via remote smtp server (gmail)

假如想象 提交于 2020-04-13 18:35:22

问题


I want to send emails via my gmail account.

My mailer config:

[
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,//set this property to false to send mails to real email addresses
'transport' => [
    'class' => 'Swift_SmtpTransport',
    'host' => 'smtp.gmail.com',
    'username' => 'my@gmail.com',
    'password' => 'pass',
    'port' => '587',
    'encryption' => 'tls',
    ],
]

I wrote command MailController:

<?php

namespace app\commands;

use yii\console\Controller;
use Yii;

/**
* Sanding mail
* Class MailController
* @package app\commands
*/
class MailController extends Controller
{
    private $from = 'my@gmail.com';
    private $to = 'to@gmail.com';

    public function actionIndex($type = 'test', $data = null)
    {
        Yii::$app->mailer->compose($type, ['data' => $data])
            ->setFrom($this->from)
            ->setTo($this->to)
            ->setSubject($this->subjects[$type])
            ->send();
    }
}

When I'm trying to run: php yii mail

I get: sh: 1: /usr/sbin/sendmail: not found

But why it requires sendmail if I want just SMTP connection to smtp.gmail.com?


回答1:


I think you have configured the mailer wrongly. Because it is still using the default mail function. From the documentation the configuration should be like below. The mailer should be inside components.

'components' => [
    ...
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'username',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
        ],
    ],
    ...
],

One more suggestion is to use port "465" and encryption as "ssl" instead of port "587", encryption "tls".




回答2:


Yii2 Has different config files for web and console works. So you need to config both of them. Regarding this issue, I had to make mail config file (for example mailer.php) and include it in both config files (web.php & console.php) like:

'components' => [
    ...
    'mailer' => require(__DIR__ . '/mailer.php'),
    ...
],



回答3:


Might be useful for someone as reference:

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'viewPath' => '@common/mail',
    'useFileTransport' => false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',
        'username' => 'username',
        'password' => 'password',
        'port' => 587,
        'encryption' => 'tls',
        'streamOptions' => [ 
            'ssl' => [ 
                'allow_self_signed' => true,
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ]
    ],
]


来源:https://stackoverflow.com/questions/29592790/yii2-swiftmailer-sending-email-via-remote-smtp-server-gmail

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