可以在localhost上使用swiftMailer发送电子邮件吗?

con*_*322 6 php swiftmailer yii2

我目前没有收到使用以下配置的电子邮件,并且想知道是否与我的设置有关可能遗漏了某些内容或者它在MAMP localhost上无法正常工作?

common-local.php在common config目录下

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        // 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' => true,
    ],
Run Code Online (Sandbox Code Playgroud)

然后发送电子邮件(显示成功消息)

public function submitreview($email)
{
    //return Yii::$app->mailer->compose(['html' => '@app/mail-templates/submitreview'])
    return Yii::$app->mailer->compose()
        ->setTo($email)
        ->setFrom([$this->email => $this->name])
        ->setSubject($this->title)
        ->setTextBody($this->description)
        ->attachContent($this->file)
        ->send();
}
Run Code Online (Sandbox Code Playgroud)

ank*_*itr 4

您可以使用以下配置通过 Yii2 中的 localhost 发送邮件。

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',  
            'username' => 'ENTER_EMAIL_ADDRESS_HERE',
            'password' => 'ENTER_PASSWORD',
            'port' => '587', 
            'encryption' => 'tls', 
        ],
    ]
Run Code Online (Sandbox Code Playgroud)

并在你的控制器中

\Yii::$app->mail->compose('your_view', ['params' => $params])
    ->setFrom([\Yii::$app->params['supportEmail'] => 'Test Mail'])
    ->setTo('to_email@xx.com')
    ->setSubject('This is a test mail ' )
    ->send();
Run Code Online (Sandbox Code Playgroud)

  • 这个本地主机怎么样?您列出的传输方式是 gmail。 (4认同)