如何在 Yii2 中使用 swiftMailer [英] How to use the swiftMailer in Yii2

查看:21
本文介绍了如何在 Yii2 中使用 swiftMailer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于无法理解如何在 Yii2 中使用 swiftMailer 扩展.就这个题目来看,我没有发现问题,任务是微不足道的,但到最后我还是看不懂.

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.

有些例子没有更详细地描述发送信件的所有周期,或者我不明白一些事情:(

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

设置

    return [
    //....
   'components' => [
    ......
    'mail' => [
      'class' => 'yiiswiftmailerMailer',
      '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();

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

附言我调整了域记录 MX、DKIM、SPF 添加.

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

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.

推荐答案

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

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.

在你的 common/main-local.php 的组件部分

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

'mail' => [
        'class' => 'yiiswiftmailerMailer',
        '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();

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

This should work! Hope this will help you!

这篇关于如何在 Yii2 中使用 swiftMailer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆