如何在Yii2中使用swiftMailer

时间:2022-09-24 08:47:35

I can't finally understand how to use the swiftMailer extension in Yii2. Judging by that on this subject I didn't find questions, the task is trivial, but up to the end I couldn't understand.

我终于搞不懂如何在Yii2中使用swiftMailer扩展。从这个问题上判断,我没有发现问题,这个任务很琐碎,但到最后我还是无法理解。

There are examples which don't describe in more detail all cycle of sending the letter or I don't understand something :(

有一些例子没有详细地描述发送信件的整个周期,或者我不理解某事:

Setup

设置

    return [
    //....
   'components' => [
    ......
    'mail' => [
      'class' => 'yii\swiftmailer\Mailer',
      'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'localhost',
        'username' => 'username',
        'password' => 'password',
        'port' => '587',
        'encryption' => 'tls',
      ],
    ],
  ]
];

Send

发送

Yii::$app->mail->compose()
->setTo($toEmail)
->setFrom([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();

I want will receive a concrete working example. Thank you.

我想要一个具体的工作例子。谢谢你!

P.S. I adjusted domain records MX, DKIM, SPF added.

我调整了领域记录MX, DKIM, SPF添加。

UPD (some answer):

乌利希期刊指南(回答):

E-mail which is passed in "From" field, it is put down by default in the field of "Return-path", has to be the existing address. Some providers don't allow sending mail from nonexistent email addresses.

在“From”字段中传递的电子邮件,在“Return-path”字段中默认设置为已存在的地址。一些提供商不允许从不存在的电子邮件地址发送邮件。

5 个解决方案

#1


42  

Make sure you have initialised your application in production environment to send emails from your application,else it will be written in to the mailoutput folder.Or manually edit the config file like follows.

确保您已经在生产环境中初始化了您的应用程序,以便从您的应用程序发送电子邮件,否则它将被写入到mailoutput文件夹。或者手动编辑配置文件,如下所示。

In the components's section of your common/main-local.php

在公共/主-本地.php的组件部分中

'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@backend/mail',
        'useFileTransport' => false,//set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'username@gmail.com',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
                        ],
    ],
    ],

In your Controller

在你的控制器

    \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();

This should work! Hope this will help you!

这应该工作!希望这能对你有所帮助!

#2


8  

You need not using SMTP transport with swiftmailer, only remove 'useFileTransport' => true in the config file (app/config/web.php in basic template) and the mails will flow.

您不需要使用带有swiftmailer的SMTP传输,只需删除配置文件(app/config/web)中的“useFileTransport”=> true。php的基本模板)和邮件将流。

Take a look in the docs:

看看这些文件:

http://www.yiiframework.com/doc-2.0/ext-swiftmailer-index.html

http://www.yiiframework.com/doc - 2.0 - / - ext - swiftmailer index . html

#3


4  

Warning: This option no longer available, as Mandrill was bought by Mailchimp

Sometimes could be issues with using SwiftMailer not dependent from you. Like when I used mail.ru e-mail server. I found solution in laravel community and implemend in Yii2.

有时候使用SwiftMailer可能会有问题,而不是依赖于您。就像我使用mail.ru电子邮件服务器一样。我在laravel社区找到了解决方案,并在Yii2中实现。

You can use alternative service like https://mandrillapp.com/ (12k email per month, 250 within hour is free) and setting up like below:

您可以使用https://mandrillapp.com/(每月12k封邮件,一小时250封免费)等替代服务,设置如下:

laravel community / setup mail with mandrill

laravel社区/安装邮件与mandrill有关

'host' => 'smtp.mandrillapp.com',
'username' => 'user@domain.name',
'password' => 'oDLKswXZIkry8634f1jCDg', // new generated API key by mandrill
'port' => '587',
'encryption' => 'tls',

If you are using gmail email you can also can face with security issue. You can swith off security by allowing application use your gmail account.

如果你正在使用gmail电子邮件,你也可以面对安全问题。您可以通过允许应用程序使用gmail帐户来取消安全性。

If you signed in with google use links below:

如果您使用谷歌登录,请使用以下链接:

https://www.google.com/settings/security/lesssecureapps

https://www.google.com/settings/security/lesssecureapps

Hope it will help somebody

希望它能帮助别人。

#4


3  

If you're using the basic template, then you would need to add

如果使用的是基本模板,则需要添加

'viewPath' => '@app/mail',

to the config

的配置

#5


1  

Actually, you have to use config key mailer instead of mail.

实际上,您必须使用配置键mailer而不是mail。

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

],

),

#1


42  

Make sure you have initialised your application in production environment to send emails from your application,else it will be written in to the mailoutput folder.Or manually edit the config file like follows.

确保您已经在生产环境中初始化了您的应用程序,以便从您的应用程序发送电子邮件,否则它将被写入到mailoutput文件夹。或者手动编辑配置文件,如下所示。

In the components's section of your common/main-local.php

在公共/主-本地.php的组件部分中

'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@backend/mail',
        'useFileTransport' => false,//set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'username@gmail.com',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
                        ],
    ],
    ],

In your Controller

在你的控制器

    \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();

This should work! Hope this will help you!

这应该工作!希望这能对你有所帮助!

#2


8  

You need not using SMTP transport with swiftmailer, only remove 'useFileTransport' => true in the config file (app/config/web.php in basic template) and the mails will flow.

您不需要使用带有swiftmailer的SMTP传输,只需删除配置文件(app/config/web)中的“useFileTransport”=> true。php的基本模板)和邮件将流。

Take a look in the docs:

看看这些文件:

http://www.yiiframework.com/doc-2.0/ext-swiftmailer-index.html

http://www.yiiframework.com/doc - 2.0 - / - ext - swiftmailer index . html

#3


4  

Warning: This option no longer available, as Mandrill was bought by Mailchimp

Sometimes could be issues with using SwiftMailer not dependent from you. Like when I used mail.ru e-mail server. I found solution in laravel community and implemend in Yii2.

有时候使用SwiftMailer可能会有问题,而不是依赖于您。就像我使用mail.ru电子邮件服务器一样。我在laravel社区找到了解决方案,并在Yii2中实现。

You can use alternative service like https://mandrillapp.com/ (12k email per month, 250 within hour is free) and setting up like below:

您可以使用https://mandrillapp.com/(每月12k封邮件,一小时250封免费)等替代服务,设置如下:

laravel community / setup mail with mandrill

laravel社区/安装邮件与mandrill有关

'host' => 'smtp.mandrillapp.com',
'username' => 'user@domain.name',
'password' => 'oDLKswXZIkry8634f1jCDg', // new generated API key by mandrill
'port' => '587',
'encryption' => 'tls',

If you are using gmail email you can also can face with security issue. You can swith off security by allowing application use your gmail account.

如果你正在使用gmail电子邮件,你也可以面对安全问题。您可以通过允许应用程序使用gmail帐户来取消安全性。

If you signed in with google use links below:

如果您使用谷歌登录,请使用以下链接:

https://www.google.com/settings/security/lesssecureapps

https://www.google.com/settings/security/lesssecureapps

Hope it will help somebody

希望它能帮助别人。

#4


3  

If you're using the basic template, then you would need to add

如果使用的是基本模板,则需要添加

'viewPath' => '@app/mail',

to the config

的配置

#5


1  

Actually, you have to use config key mailer instead of mail.

实际上,您必须使用配置键mailer而不是mail。

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

],

),