编程语言
首页 > 编程语言> > php-发送邮件-Yii 2.0

php-发送邮件-Yii 2.0

作者:互联网

我正在尝试发送邮件,但出现错误提示.

Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: yii\web\Application::mail

将Yii :: $app-> mail-> compose()更改为Yii :: $app-> mailer-> compose()
我得到这个错误

Invalid Parameter – yii\base\InvalidParamException

Invalid path alias:@backend/mail

我没弄错我在做错什么.

我正在使用yii-app-basic.

config / console.php

...
    'components' => [
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
        ],
    ],
...

config / web.php

...
    'components' => [
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@backend/mail',
            'useFileTransport' => true,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'localhost',
                'username' => 'root',
                'password' => '',
                'port' => '8080',
                'encryption' => 'tls',
            ],
        ],
    ],
...

SiteController.php

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\swiftmailer\Mailer;

class SiteController extends Controller
{
    public function actionRegister()
    {
        Yii::$app->mail->compose()
                ->setFrom('enamRaj@gmail.com')
                ->setTo('Raj@infotech.com')
                ->setSubject('This is a test mail')
                ->send();
    }
}

错误截图
enter image description here

我是Yii的新手.我没什么主意如果这是一个愚蠢的问题,请原谅我.

Mailing- Yii 2.0得到帮助.但是,没有太多想法.
请帮我发送电子邮件

解决方法:

使用邮件程序组件.

Yii::$app->mailer->compose()

在您的配置中,您将编写在Yii :: $app应用程序上可用的组件.

例:

在配置中:

'components' => [
    'myComponent' => ['class' => '\common\MyClass']
]

在控制器中:

Yii::$app->myComponent->foo();

标签:yii2-basic-app,yii2,email,php
来源: https://codeday.me/bug/20191119/2037773.html