【问题标题】:How to use the swiftMailer in Yii2Yii2中如何使用swiftMailer
【发布时间】:2014-09-19 15:54:30
【问题描述】:

我最终无法理解如何在 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();

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

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

UPD(一些答案)

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

【问题讨论】:

    标签: php yii swiftmailer yii2


    【解决方案1】:

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

    在你的 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();
    

    这应该可以!希望对您有所帮助!

    【讨论】:

    • 谢谢!我已经找到了答案。看来,一些提供商只收到“返回路径”字段中的现有电子邮件
    • 看密码重置的例子,应该是mailer,而不是mail yourApp->frontend->models->PasswordResetRequestForm.php
    • 嗨 Dency GB,我是 yii2 和框架的新手。您能否解释一下我应该在哪里输入为控制器建议的代码。我应该将它放在一个新的控制器/动作下,以及如何使用它或将它放在创建或更新下。谢谢。
    【解决方案2】:

    您不需要使用 swiftmailer 的 SMTP 传输,只需删除配置文件中的'useFileTransport' => true(基本模板中的app/config/web.php),邮件就会流动。

    查看文档:

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

    【讨论】:

      【解决方案3】:

      警告:此选项不再可用,因为 Mandrill 已被 Mailchimp 收购

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

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

      laravel community / setup mail with 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

      希望对大家有所帮助

      【讨论】:

      • 不再是免费服务。
      【解决方案4】:

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

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

      到配置

      【讨论】:

        【解决方案5】:

        Google 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";
            }
        }
        

        【讨论】:

          【解决方案6】:

          实际上,您必须使用配置键 ma​​iler 而不是 ma​​il

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-04-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多