I have a trouble.
I install a yii2-user module by folowing link https://github.com/dektrium/yii2-user/tree/0.9.9
When I trying to register I got a success message, but I not receiving confirmation email.
I used OpenServer
config/web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'language' => 'ru-RU',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'modules' => [
'user' => [
'class' => 'dektrium\user\Module',
'mailer' => [
'sender' => ['name#gmail.com' => 'Vlad'], // or ['no-reply#myhost.com' => 'Sender name']
'welcomeSubject' => 'Welcome subject',
'confirmationSubject' => 'Confirmation subject',
'reconfirmationSubject' => 'Email change subject',
'recoverySubject' => 'Recovery subject',
],
],
],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'QqvFfvH3g3PwMMu_bRRHB4Qz0uPJwiB-',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
// 'user' => [
// 'identityClass' => 'app\models\User',
// 'enableAutoLogin' => true,
// ],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// 'transport' => [
// 'class' => 'Swift_SmtpTransport',
// 'host' => 'smtp.gmail.com',
// 'username' => 'name#gmail.com',
// 'password' => 'mypassword',
// 'port' => '587',
// 'encryption' => 'tls',
// ],
// 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' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
Untill your environment is in dev mode, you will never receive the emails.
You have 3 differents way for read the emails:
the dev bar in bottom, if there is not any redirect after send email, you can open and find it there
in the server, looking for runtime/mail/ folder and there will be the sended emails
deploy enviroment from developer to production
The mailer component should be at the same level as model component.
You can configure it as follows:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#app/mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.mailtrap.io', // your host, here using fake email server (https://mailtrap.io/). You can use gmail: 'host' => 'smtp.gmail.com'
'username' => 'your host username',
'password' => 'your host password',
'port' => '2525',
'encryption' => 'tls',
],
],
Related
I am having troubles with the Mailer after upgrading from CakePHP 3 to 4.
This is the relevant part my configuration:
<?php
return [
'EmailTransport' => [
'default' => [
'className' => 'Mail',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'password',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'cronjob' => [
'className' => 'Mail',
],
'accounts' => [
'className' => 'Mail',
],
],
'Email' => [
'default' => [
'transport' => 'default',
'from' => 'you#localhost',
],
'cronjob' => [
'transport' => 'cronjob',
'from' => 'cronjob#foobar.com',
],
'accounts' => [
'transport' => 'accounts',
'from' => 'accounts#foobar.com',
],
],
];
This is the snippet that's causing the error:
private function sendActivationEmail(User $user)
{
$url = Router::url([
'prefix' => 'Admin',
'plugin' => 'UserManager',
'controller' => 'Users',
'action' => 'activate',
$user->username,
$user->activation_key,
], true);
debug(Configure::read('EmailTransport'));
debug(Configure::read('Email'));
$mailer = new Mailer('accounts');
$mailer->setFrom(['accounts#foobar.com' => 'Foobar Website Manager'])
->setTo($user->email, $user->fullName)
->setSubject('Please activate your account')
->setEmailFormat('html')
->setViewVars(compact('url', 'user'))
->viewBuilder()
->setTemplate('UserManager.register');
return $mailer->deliver();
}
The error is Unknown email configuration "accounts"., thrown in
The output of the two debug functions is the following:
/vendor/plugins/usermanager/src/Model/Table/UsersTable.php (line 72)
[
'default' => [
'className' => 'Mail'
],
'cronjob' => [
'className' => 'Mail'
],
'accounts' => [
'className' => 'Mail'
]
]
/vendor/plugins/usermanager/src/Model/Table/UsersTable.php (line 73)
[
'default' => [
'transport' => 'default',
'from' => 'something#foobar.com'
],
'cronjob' => [
'transport' => 'cronjob',
'from' => 'cronjob#foobar.com'
],
'accounts' => [
'transport' => 'accounts',
'from' => 'accounts#foobar.com'
]
]
So it seems like the accounts key is present in the mail configuration, then why am I getting this error?
Make sure that you have upgraded your bootstrap.php accordingly along the way, specifically with regards to how EmailTransport and Email are being consumed, this was introduced with CakePHP 3.7 and 4.1 if I'm not mistaken:
TransportFactory::setConfig(Configure::consume('EmailTransport'));
Mailer::setConfig(Configure::consume('Email'));
https://github.com/cakephp/app/blob/4.2.2/config/bootstrap.php#L163-L164
I have configured mailer in this way
In the component
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'gmailaccount',
'password' => 'gmailpassword',
'port' => '587',
'encryption' => 'tls',
],
],
],
],
In my controller i have
public function actionTestmail(){
return \Yii::$app->mailer->compose('testmail')
->setFrom([Yii::$app->params['supportEmail']]) //this is set in params
->setTo("mysecondmail#gmail.com")
->setSubject('Testing yii2 mailer ')
->send();
}
The above always returns an error of setting unknown property: yii\swiftmailer\Mailer::mailer, what could be wrong,
The above configuration is a copy paste from yii2 website yet it fails to work
You have two times repeated mailer in components configuration.
This is right configuration:
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'gmailaccount',
'password' => 'gmailpassword',
'port' => '587',
'encryption' => 'tls',
],
],
],
I installed the extension reportico through composer and already web.php configuration file, but when I go into Reportico Admin Page error instead. The solution how ?
enter image description here
scrip web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'K1pkkP_iwACp933A03LotJ3AfRsyIb-D',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// 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,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'reportico' => [
'class' => 'reportico\reportico\Module' ,
'controllerMap' => [
'reportico' => 'reportico\reportico\controllers\ReporticoController',
'mode' => 'reportico\reportico\controllers\ModeController',
'ajax' => 'reportico\reportico\controllers\AjaxController',
]
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
Move reportico to components array:
'components' => [
//...
'reportico' => [
'class' => 'reportico\reportico\Module' ,
'controllerMap' => [
'reportico' => 'reportico\reportico\controllers\ReporticoController',
'mode' => 'reportico\reportico\controllers\ModeController',
'ajax' => 'reportico\reportico\controllers\AjaxController',
]
]
]
I have define a module named `admin' in yii2.
I have registered this module in web.php config file.
But when I try to access this I face with an error.
here is my web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'language'=>'fa_ir',
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'SiMhYyhP2MZ_BAi321KfDHFk5uleQFa9',
],
'jdate' => [
'class' => 'jDate\DateTime'
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'modules' => [
'admin' => [
'class' => 'app\modules\admin\Module',
],
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// 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' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'pishevaris#gmail.com',
'password' => 'bhtk1368',
'port' => '587',
'encryption' => 'tls',
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'db' => require(__DIR__ . '/db.php'),
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
here is my Module class:
<?php
namespace app\modules\admin;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\admin\controllers';
public function init()
{
parent::init();
// custom initialization code goes here
}
}
but when I try to access mysite/admin/default/index I face The configuration for the "modules" component must contain a "class" element error
Please define module in out side of component. Like
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'language'=>'fa_ir',
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'SiMhYyhP2MZ_BAi321KfDHFk5uleQFa9',
],
'jdate' => [
'class' => 'jDate\DateTime'
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// 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' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'pishevaris#gmail.com',
'password' => 'bhtk1368',
'port' => '587',
'encryption' => 'tls',
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'db' => require(__DIR__ . '/db.php'),
],
'modules' => [
'admin' => [
'class' => 'app\modules\admin\Module',
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
Modules are different with Components, and you defined your module as a component by adding this to component array. you should write your module after or before component. like below:
['components'] => [],
['modules'] => [],
I'm going through Security-Authorization Tutorial to configure authManager using DbManager.
After declaring the below code in web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'tYXyeisqgn9Qn_baaI6JRV4a6NY54nrq',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// 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.
'viewPath' => '#backend/mail',
'useFileTransport' => true,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost',
'username' => 'root',
'password' => '',
'port' => '8080',
'encryption' => 'tls',
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
And, this code in console.php
<?php
Yii::setAlias('#tests', dirname(__DIR__) . '/tests');
$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');
return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'app\commands',
'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
],
'params' => $params,
];
config/db.php
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=danishYii',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
I typed ./yii migrate --migrationPath=vendor/yiisoft/yii2/rbac/migrations
in my terminal. This command i got from Component is not getting loaded -Stack overflow
I got this error in my terminal
Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002]
Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)'
I'm very new to Yii. So please don't mind if this is a silly question.
Please help me to rectify this problem.
Check if in your
console/config/main.php
you have a proper configuration for db access like this sample for yii2-app-advanced template
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=your_hostname;dbname=your_dbname',
'username' => 'your_username',
'password' => 'your_password',
'charset' => 'utf8',
'enableSchemaCache' => true,
],
for basic template
be sure you have in basic/console/config.php a reference to db in component secion like this
return [
.......
'components' => [
......
'db' => require(__DIR__ . '/db.php'),
],
......
];
and then in basci/config/db.php a properly db config
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=your_hostname;dbname=your_dbname',
'username' => 'your_username',
'password' => 'your_password',
'charset' => 'utf8',
];