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

查看:239
本文介绍了如何在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' => '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.

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.

推荐答案

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

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' => '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();

这应该有效!希望这会对您有所帮助!

This should work! Hope this will help you!

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

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