如何在 Yii2 中使用 swiftMailer

2023-08-18php开发问题
3

本文介绍了如何在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17