php 如何在 Yii2 中使用 swiftMailer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24995620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:35:20  来源:igfitidea点击:

How to use the swiftMailer in Yii2

phpyiiswiftmaileryii2

提问by frops

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.

PS 我调整了域记录 MX、DKIM、SPF 添加。

UPD (some answer):

UPD(一些答案)

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.

在“发件人”字段中传递的电子邮件,在“返回路径”字段中默认放置,必须是现有地址。某些提供商不允许从不存在的电子邮件地址发送邮件。

回答by Dency G B

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.

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

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

在您的 common/main-local.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' => '[email protected]',
            '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('[email protected]')
    ->setSubject('This is a test mail ' )
    ->send();

This should work! Hope this will help you!

这应该有效!希望能帮到你!

回答by uselma

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

您不需要在 swiftmailer 中使用 SMTP 传输,只需'useFileTransport' => true在配置文件(app/config/web.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

回答by sambua

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

警告:此选项不再可用,因为 Mandrill 已被 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 封电子邮件,1 小时内 250 封免费)并进行如下设置:

laravel community / setup mail with mandrill

laravel 社区/使用 mandrill 设置邮件

'host' => 'smtp.mandrillapp.com',
'username' => '[email protected]',
'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

希望它会帮助某人

回答by Martin

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

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

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

to the config

到配置

回答by MichalB

Actually, you have to use config key mailerinstead of mail.

实际上,您必须使用 config key mailer而不是mail

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

回答by Ram Pukar

Google gmail security option

Google Gmail 安全选项

https://myaccount.google.com/lesssecureapps

Project file path

项目文件路径

config\web.php
'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'useFileTransport' => false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',  
        'username' => '[email protected]',
        'password' => 'email_password',
        'port' => '465',
        'encryption' => 'ssl',
        'streamOptions' => [ 
            'ssl' => [ 
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
            ],
        ]
    ]
],

Add function inside your controller

在控制器中添加功能

public function actionSend() {
    $send = Yii::$app->mailer->compose()
    ->setFrom('[email protected]')
    ->setTo('[email protected]')
    ->setSubject('Test Message')
    ->setTextBody('Plain text content. YII2 Application')
    ->setHtmlBody('<b>HTML content <i>Ram Pukar</i></b>')
    ->send();
    if($send){
        echo "Send";
    }
}