如何在Yii2中使用swiftMailer

41

我最终无法理解如何在Yii2中使用SwiftMailer扩展。鉴于这一主题上我没有找到问题,任务是微不足道的,但最终我仍然无法理解。

有些示例没有更详细地描述发送邮件的整个过程,或者我没有理解其中的某些内容 :(

安装

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

发送

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

我希望能收到一个具体的工作示例。谢谢。

P.S. 我已调整域记录 MX、DKIM、SPF。

更新(某些答案)

在“发件人”字段中传递的电子邮件默认会放在“Return-path”字段中,必须是现有地址。一些提供商不允许从不存在的电子邮件地址发送邮件。

6个回答

51

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

在common/main-local.php的components部分中

'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',
            ],
    ],

在你的控制器中

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

这应该可以工作!希望这能帮到你!


谢谢!我已经找到了答案。看起来,一些提供商只接收“返回路径”字段中存在的电子邮件。 - frops
查看密码重置示例,应该是mailer而不是mail吗? yourApp->frontend->models->PasswordResetRequestForm.php - johnsnails
嗨,Dency G B,我是yii2和框架的新手。您能否友好地解释一下我应该在哪里输入建议的控制器代码?我应该将其放置在新的控制器/操作下,如何使用它或将其放置在创建或更新下。谢谢。 - Joshi

10

5

警告:由于Mandrill被Mailchimp收购,此选项不再可用

有时使用SwiftMailer可能会出现问题,这与您无关。就像我使用mail.ru电子邮件服务器时遇到的问题一样。 我在laravel社区中找到了解决方案,并在Yii2中实施。

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

laravel社区/使用mandrill设置邮件

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

如果您正在使用Gmail电子邮件,您也可能会遇到安全问题。您可以通过允许应用程序使用您的Gmail帐户来关闭安全性。
如果您使用Google登录,请使用下面的链接:

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

希望能帮助到某些人


1
不再是免费服务。 - crafter

5
如果您正在使用基本模板,则需要添加:
'viewPath' => '@app/mail',

到配置文件


2
谷歌Gmail安全选项。
https://myaccount.google.com/lesssecureapps

项目文件路径
config\web.php
'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'useFileTransport' => false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',  
        'username' => 'email_address@gmail.com',
        'password' => 'email_password',
        'port' => '465',
        'encryption' => 'ssl',
        'streamOptions' => [ 
            'ssl' => [ 
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
            ],
        ]
    ]
],

在你的控制器中添加函数。
public function actionSend() {
    $send = Yii::$app->mailer->compose()
    ->setFrom('from_mail@gmail.com')
    ->setTo('to_mail@gmail.com')
    ->setSubject('Test Message')
    ->setTextBody('Plain text content. YII2 Application')
    ->setHtmlBody('<b>HTML content <i>Ram Pukar</i></b>')
    ->send();
    if($send){
        echo "Send";
    }
}

2
实际上,您需要使用配置密钥 mailer 而不是 mail
'components' => [
...
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'localhost',
            'username' => 'username',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
        ],
    ],
...
],

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接