Nana Partykar
Nana Partykar

Reputation: 10548

Sending Mail - Yii 2.0

I'm trying to send mail, but getting error like.

Unknown Property – yii\base\UnknownPropertyException

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

After changing Yii::$app->mail->compose() to Yii::$app->mailer->compose() I got this error

Invalid Parameter – yii\base\InvalidParamException

Invalid path alias:@backend/mail

I'm not getting where i'm doing mistake.

I'm using 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('[email protected]')
                ->setTo('[email protected]')
                ->setSubject('This is a test mail')
                ->send();
    }
}

Error Screenshot enter image description here

I'm new to Yii. I don't have much idea. If this is a silly question,please forgive me.

Taking help from Mailing- Yii 2.0. But, not getting much idea. Please help me to send email

Upvotes: 3

Views: 3817

Answers (3)

Ammar Dje
Ammar Dje

Reputation: 90

well it's an old question but if you still using yii if you want to send a mail with no effort use dektrium it's an abandoned project but it provides many features user management email login etc

Upvotes: -1

hesselek
hesselek

Reputation: 181

You are using the basic template. Path '@backend/mail' is only for advanced template. The correct path is @app/mail (or whatever path where you are storing your email templates).

Upvotes: 0

Onedev_Link
Onedev_Link

Reputation: 2012

Use mailer component.

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

In your config you write components that will be avalible on Yii::$app application.

Example:

In config:

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

In controller:

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

Upvotes: 4

Related Questions