我想通過我的Gmail賬戶發送郵件。Yii2 SwiftMailer通過遠程SMTP服務器發送郵件(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' => '[email protected]',
'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 = '[email protected]';
private $to = '[email protected]';
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
但爲什麼它需要sendmail,如果我只想SMTP連接smtp.gmail .COM?
你確定這封郵件組件配置是關鍵「郵件」?根據這個配置,它甚至不應該嘗試發送電子郵件爲''useFileTransport'=> true「意味着它只保存消息到文件。 –