Yii2 SwiftMailer 通过远程 smtp 服务器(gmail)发送电子邮件 [英] Yii2 SwiftMailer sending email via remote smtp server (gmail)
问题描述
我想通过我的 Gmail 帐户发送电子邮件.
我的邮件程序配置:
<预><代码>['类' =>'yii\swiftmailer\Mailer','useFileTransport' =>false,//将此属性设置为 false 以将邮件发送到真实的电子邮件地址'运输' =>['类' =>'Swift_SmtpTransport','主机' =>'smtp.gmail.com','用户名' =>'我的@gmail.com','密码' =>'经过','端口' =>'587','加密' =>'tls',],]我写了命令MailController:
mailer->compose($type, ['data' => $data])->setFrom($this->from)->setTo($this->to)->setSubject($this->subjects[$type])->发送();}}
当我尝试运行时:php yii mail
我得到:sh: 1:/usr/sbin/sendmail: not found
但是如果我只想通过 SMTP 连接到 smtp.gmail.com,为什么它需要 sendmail?
我认为您错误地配置了邮件程序.因为它仍然使用默认的邮件功能.从 文档 中,配置应该如下所示.邮件程序应该在 components
内.
'components' =>[...'邮递员' =>['类' =>'yii\swiftmailer\Mailer','运输' =>['类' =>'Swift_SmtpTransport','主机' =>'smtp.gmail.com','用户名' =>'用户名','密码' =>'密码','端口' =>'587','加密' =>'tls',],],...],
另外一个建议是使用端口465"和加密为ssl",而不是端口587",加密为tls".
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?
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".
这篇关于Yii2 SwiftMailer 通过远程 smtp 服务器(gmail)发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!