Php 如何在Yii2中使用快捷邮递器

Php 如何在Yii2中使用快捷邮递器,php,yii,swiftmailer,yii2,Php,Yii,Swiftmailer,Yii2,我最终无法理解如何在Yii2中使用swiftMailer扩展。从这个问题上看,我没有发现问题,任务很琐碎,但到最后我还是无法理解 有一些例子没有更详细地描述发送信函的整个过程,或者我不理解一些事情:( 设置 return [ //.... 'components' => [ ...... 'mail' => [ 'class' => 'yii\swiftmailer\Mailer', 'transport' =>

我最终无法理解如何在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();
我希望收到一个具体的工作示例。谢谢

PSI调整了域记录MX、DKIM、SPF添加

UPD(一些答案)

在“发件人”字段中传递的电子邮件,默认情况下会将其放在该字段中 “返回路径”的地址必须是现有地址 允许从不存在的电子邮件地址发送邮件


确保您已在生产环境中初始化应用程序以从应用程序发送电子邮件,否则它将写入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();

这应该行得通!希望这对您有所帮助!

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

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

警告:此选项不再可用,因为Mandrill已被Mailchimp购买 有时可能是使用SwiftMailer的问题,而不是依赖于您。例如,当我使用mail.ru电子邮件服务器时。 我在laravel社区找到了解决方案,并在Yii2中实现

您可以使用其他服务,如(每月发送12k电子邮件,一小时内免费发送250封)和如下设置:

若你们使用的是gmail电子邮件,你们也可能面临安全问题。你们可以通过允许应用程序使用你们的gmail帐户来关闭安全

如果您使用谷歌登录,请使用以下链接:


希望它能帮助某人

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

查看文档:


实际上,您必须使用config键mailer而不是mail

'components' => [
...
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'localhost',
            'username' => 'username',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
        ],
    ],
...
],
谷歌gmail安全选项

项目文件路径

在控制器中添加函数

公共函数actionSend(){
$send=Yii::$app->mailer->compose()
->setFrom('from_mail@gmail.com')
->seto('to_mail@gmail.com')
->setSubject('测试消息')
->setTextBody('纯文本内容.YII2应用程序')
->setHtmlBody('HTML内容Ram Pukar')
->send();
如果($send){
回音“发送”;
}
}

谢谢!我已经找到了答案。似乎有些提供商只接收“返回路径”字段中的现有电子邮件看看密码重置的例子,应该是
mailer
,而不是
mail
yourApp->frontend->models->PasswordResetRequestForm.phpHi Dency G B,我对yii2和framework不熟悉。你能解释一下我应该在哪里输入建议控制器的代码吗?我应该把它放在一个新的控制器/操作下,以及如何使用它吗将其置于“创建”或“更新”下。谢谢。不再是免费服务。
'components' => [
...
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'localhost',
            'username' => 'username',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
        ],
    ],
...
],
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";
    }
}