Yii2 SwiftMailer 通过远程 smtp 服务器 (gmail) 发送电子邮件
Yii2 SwiftMailer sending email via remote smtp server (gmail)
我想通过我的 gmail 帐户发送电子邮件。
我的邮件程序配置:
[
'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',
],
]
我写了命令 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();
}
}
当我尝试 运行 时:php yii mail
我得到:sh: 1: /usr/sbin/sendmail: not found
但是,如果我只想与 smtp.gmail.com 建立 SMTP 连接,为什么还需要 sendmail?
我认为您错误地配置了邮件程序。因为它还在使用默认的邮件功能。从 documentation 配置应该如下所示。邮件程序应该在 components
.
里面
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'username',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
],
],
...
],
另一个建议是使用端口“465”和加密方式 "ssl" 而不是端口“587”,加密方式 "tls".
Yii2 有不同的 Web 和控制台配置文件。所以你需要配置他们两个。关于这个问题,我必须制作邮件配置文件(例如 mailer.php
)并将其包含在两个配置文件(web.php 和 console.php)中,例如:
'components' => [
...
'mailer' => require(__DIR__ . '/mailer.php'),
...
],
可能对某人有用作为参考:
'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,
],
]
],
]
我想通过我的 gmail 帐户发送电子邮件。
我的邮件程序配置:
[
'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',
],
]
我写了命令 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();
}
}
当我尝试 运行 时:php yii mail
我得到:sh: 1: /usr/sbin/sendmail: not found
但是,如果我只想与 smtp.gmail.com 建立 SMTP 连接,为什么还需要 sendmail?
我认为您错误地配置了邮件程序。因为它还在使用默认的邮件功能。从 documentation 配置应该如下所示。邮件程序应该在 components
.
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'username',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
],
],
...
],
另一个建议是使用端口“465”和加密方式 "ssl" 而不是端口“587”,加密方式 "tls".
Yii2 有不同的 Web 和控制台配置文件。所以你需要配置他们两个。关于这个问题,我必须制作邮件配置文件(例如 mailer.php
)并将其包含在两个配置文件(web.php 和 console.php)中,例如:
'components' => [
...
'mailer' => require(__DIR__ . '/mailer.php'),
...
],
可能对某人有用作为参考:
'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,
],
]
],
]