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('enamRaj#gmail.com')
->setTo('Raj#infotech.com')
->setSubject('This is a test mail')
->send();
}
}
Error Screenshot
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
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();
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).
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
Related
I have a yiisoft/yii2-app-advanced installed. There are three main folders - common, frontend, backend. The folder for emails is in common/mail folder. If I try to send email from frontend controller on register user it throws me an error:
The view file does not exist:
C:\wamp64-3-2-0\www\yii2\frontend/mail\emailVerify-html.php
Registration is generated via console command. But is obvious that the path to mail folder is wrong. I found a workaround to it via
\Yii::$app->mailer->htmlLayout = '#common/mail/layouts/html';
\Yii::$app->mailer->textLayout = '#common/mail/layouts/text';
but it does not look very good. Is it possible to set it up in config file? Hope so. Thanks.
SO as I found out there is an option in config file named viewPath which is not mentioned in documentation.
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'encryption' => 'tls',
'host' => 'localhost',
'port' => '587',
'username' => 'your_username',
'password' => 'your_password',
],
],
I am facing some issue with cron job in Yii2 application.
I am define Controller in console as follow
namespace console\controllers;
use yii\console\Controller;
/**
* Job controller
*/
class JobController extends Controller {
public function actionIndex() {
echo "cron service runnning";
mail("mail#gmail.com","Cron",'Testing');
}
}
Path in cpenal is
php /home/user/public_html/root/ yii job
I always receiving email with error
Status: 404 Not Found
X-Powered-By: PHP/5.5.38
Content-type: text/html
No input file specified.
I think there is issue with cmd supplied for it.
Please help me to sort out this issue.
Thankyou
1.show system crontab log
why you do not use yii\swiftmailer\Mailer
$components = [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.xxx.com',
'username' => 'xx#xxx.com',
'password' => 'xxx',
'port' => '25',
'encryption' => 'tls',
],
'messageConfig' => [
'charset' => 'UTF-8',
'from' => ['xx#xx.com' => 'contents']
],
]
];
Yii::$app->setComponents($components);
$mail= Yii::$app->mailer->compose();
$mail->setTo(['from#xxx.com', 'to#xx.com']);
$mail->setSubject($subject);
$mail->setHtmlBody($content);
$mail->send();
I get this error when i'am using yii2 with a postgresql database.
SQLSTATE[HY000] [2002] No such file or directory
Caused by: PDOException
SQLSTATE[HY000] [2002] No such file or directory
I configured the file main-local.php like this :
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=dbname',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
],
];
By the way when i use mysql it's working .
This is obviously a configuration problem.
Call the requirements page provided by yii2 and check if the PDO Postgresql extension is installed.
Get your config right. PostgreSQL is different to MySQL. Every Database Cluster contains at least one named database. A database contains at least one named schema which in turn contain tables.
With that knowledge, your main-local.php should look like this:
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=YOURDATABASE',
'username' => 'YOURPOSTGRESUSERNAME',
'password' => 'YOURPOSTGRESPASSWORD',
'charset' => 'utf8',
'schemaMap' => [
'pgsql' => [
'class' => 'yii\db\pgsql\Schema',
'defaultSchema' => 'public' //specify your schema here, public is the default schema
]
], // PostgreSQL
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
],
];
im using Yii2 and i want to config mailer parameters geting the data from db.
Example:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'enableSwiftMailerLogging' =>true,
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => $model->getSmtpHost(),
'username' => $model->getSmtpUser(),
'password' => $model->getSmtpPass(),
'port' => $model->getSmtpPort(),
'encryption' => $model->getSmtpEncryption(),
],
]
but from web.php can't call methods from models, i tried but throws a error
Yii initialized application from this config. You can't use yii2 before yii2 is runned.
$application = new yii\web\Application($config);
As alternative you can configure mailer after create application in bootstrap.php file like this: Yii::$app->set('mailer', (new MailerConfigurator())->getConfig());
thanks to #Onedev.Link and #arogachev for his answer.that gave me an idea and i solve the problem.
i solve the problem modyfing swiftmailer component, in Mailer.php added this:
use app\models\Administracion; //The model i needed for access bd
class Mailer extends BaseMailer
{
...
...
//this parameter is for the config (web.php)
public $CustomMailerConfig = false;
...
...
...
/**
* Creates Swift mailer instance.
* #return \Swift_Mailer mailer instance.
*/
protected function createSwiftMailer()
{
if ($this->CustomMailerConfig) {
$model = new Administracion();
$this->setTransport([
'class' => 'Swift_SmtpTransport',
'host' => $model->getSmtpHost(),
'username' => $model->getSmtpUser(),
'password' => $model->getSmtpPass(),
'port' => $model->getSmtpPort(),
'encryption' => $model->getSmtpEncryption(),
]);
}
return \Swift_Mailer::newInstance($this->getTransport());
}
And in Web.php added this:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'enableSwiftMailerLogging' =>true,
'CustomMailerConfig' => true, //if its true use the bd config else set the transport here
'useFileTransport' => false,
],
This is my mailer component.
As you can see, because of my test purposes, I'm using email on file
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => true,
],
This is my log component.
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
// for this target, see http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
'class' => 'yii\log\EmailTarget',
'levels' => ['error'],
'categories' => ['yii\db\*', 'email_test'],
'message' => [
'from' => ESHOP_EMAIL,
'to' => DEVELOPER_EMAIL,
'subject' => 'Errore db [' . YII_ENV . ']',
],
],
],
],
In siteControlle->actionIndex() I'm testing log and email component in this way
public function actionIndex()
{
Yii::error("testing mail log", "email_test");
Yii::$app->mailer->compose()
->setFrom([ESHOP_EMAIL => ESHOP])
->setTo( DEVELOPER_EMAIL)
->setSubject("actionIndex executed on time " . date ("H:i:s"))
->setTextBody("Useless body")
->send();
return $this->render('index');
}
As i expect, at every reload of the index page I got 2 .eml files created inside frontend/runtime/mail folder.
So SwiftMailer is working, and even the log system.
Now the problem
I try to remove use of file from mailer component, commenting the row
'useFileTransport' => true,
When I reload the index page, I got the SECOND MAIL, the one manually composed and sent, but I DO NOT RECEIVE the first mail, the one that should be automatically composed and sent by log system using SwiftMailer.
What's wrong?
Sorry to everyone.
The problem was a mistake in the destination address. Using the logger the error caused email cannot be sent, but using the manual sending the destination address error is simply ignored and the email is sent.
I think the default value for 'useFileTransport' is true. Is better set to false and don't comment it
If you don't use a php function for sending eMail you need a transport like this
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
//'useFileTransport' => true,
'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' => 'your_mail#gmail.com',
'password' => 'your_password',
'port' => '587',
'encryption' => 'tls',
],
And if you don't use gmail you need the configure the parameter in a proper way.