【问题标题】:Yii2 Mailer isn't actually sending emailsYii2 Mailer 实际上并没有发送电子邮件
【发布时间】:2016-11-01 12:35:22
【问题描述】:

我按照 Yii2 教程来发送带附件的电子邮件, (如果需要,请参阅下面的链接)

https://www.youtube.com/watch?v=c5pebmTUQjs&index=21&list=PLRd0zhQj3CBmusDbBzFgg3H20VxLx2mkF

它在系统读取我要发送的电子邮件信息和附件并将信息和附件链接保存在数据库中的级别上工作。

但是当我尝试向真实收件人发送电子邮件并更改邮件配置时,然后尝试创建新电子邮件 有问题:

无效配置 – yii\base\InvalidConfigException 设置未知属性:Swift_MailTransport::host

这是邮件程序的配置:

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'viewPath' => '@app/mail',
    'useFileTransport' => false,
    'transport' => [
                    'host' => 'smtp.live.com',
                    'username' => 'username@live.com',
                    'password' => 'password',
                    'port' => '587',
                    'encryption' => 'tls',
                   ]
],

这里是 EmailsController 的 actionCreate 部分:

public function actionCreate()
    {
        $model = new Emails();

        if ($model->load(Yii::$app->request->post())) 
        {
            $model->attachment= UploadedFile::getInstance($model,'attachment');

            if ($model->attachment)
            {
                $time=time();
                $model->attachment->saveAs ('attachments/' .$time.'.' .$model->attachment->extension);
                $model->attachment='attachments/'.$time.'.'.$model->attachment->extension;
            }

            if ($model->attachment)
            {
                $value= Yii::$app->mailer->compose()
                ->setFrom (['sharqpress@hotmail.com'=>'Al-Sharq Printing Press'])
                ->setTo ($model->reciever_email)
                ->setSubject ($model->subject)
                ->setHtmlBody ($model->content)
                ->attach ($model->attachment)
                ->send();
            }

            else
            {
                $value= Yii::$app->mailer->compose()
                ->setFrom (['sharqpress@hotmail.com'=>'Al-Sharq Printing Press'])
                ->setTo ($model->reciever_email)
                ->setSubject ($model->subject)
                ->setHtmlBody ($model->content)
                ->send();
            }

            $model->save();
            return $this->redirect(['view','id'=>$model->id]);}
            else
                return $this -> render('create',['model'=>$model,]);
        }  

【问题讨论】:

    标签: yii2 html-email swiftmailer


    【解决方案1】:

    您的配置需要额外的类:

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@app/mail',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport', // <-- here
            'host' => 'smtp.live.com',
            'username' => 'username@live.com',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
        ]
    ],
    

    【讨论】:

    • 我看到了很多参考资料,但没有包含其他课程。如果它真的需要另一个类,它是什么或它代表什么?见文档链接:yiiframework.com/doc-2.0/yii-swiftmailer-mailer.html
    • 默认使用Swift_MailTransport。这是 SwiftMailer 的传输类,如 here 所述。您可以在 yii2-swiftmailer 扩展的 Mailer 类中看到这一点。
    猜你喜欢
    • 2012-06-28
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多