Yii2 Mailer isn't actually sending emails - yii2

I followed a Yii2 tutorial for sending e-mails with attachment,
(See the link below if you need)
https://www.youtube.com/watch?v=c5pebmTUQjs&index=21&list=PLRd0zhQj3CBmusDbBzFgg3H20VxLx2mkF
And it worked on a level that the system reads the email info and the attachment that I want to send, and saves the info and attachment link in the db.
But when I tried to send emails to a real recipient and changed the mailer config, then tried to create a new email
there was a problem:
Invalid Configuration – yii\base\InvalidConfigException
Setting unknown property: Swift_MailTransport::host
Here is the config for mailer:
'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',
]
],
And here is the actionCreate part of the EmailsController:
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,]);
}

Your configuration needs additional class:
'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',
]
],

Related

Yii2 module or controller routing

I try use routing for module and controller
'<controller>'=>'<controller>/index',
'<controller>/<id:\d+>'=>'<controller>/view',
'<controller>/<action>'=>'<controller>/<action>',
'<module>' => '<module>/default/index',
'<module>/<id:\d+>' => '<module>/default/view',
'<module>/edit/<id:\d+>' => '<module>/default/edit',
'<module>/delete/<id:\d+>' => '<module>/default/delete',
'<module>/<action>' => '<module>/default/<action>',
'<module>/<controller>' => '<module>/<controller>/index',
'<module>/<controller>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module>/<controller>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module>/<controller>/<id:\d+>' => '<module>/<controller>/view',
'<module>/<controller>/<action>' => '<module>/<controller>/<action>',
but for some reason, if the first matching rule did not work, then the rest are not checked, you get a 404 error.
For example, if the controller is not found, then 404 is obtained, and the check does not reach the modules.
url manager config
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'store/upload'=>'store/upload',
'stores'=>'store/index',
// 'post/<id:\d+>'=>'post/view',
'comment/send'=>'comment/send',
['class' => 'yii\rest\UrlRule', 'controller' => 'Api', 'prefix' => 'api'],
'api/<id:\d+>'=>'api/view',
[
'pattern' => 'users',
'route' => 'users/index',
'suffix' => '/',
'normalizer' => false, // отключаем нормализатор для этого правила
],
// '<controller>'=>'<controller>/index',
// '<controller>/<id:\d+>'=>'<controller>/view',
// '<controller>/<action>'=>'<controller>/<action>',
'<module>' => '<module>/default/index',
'<module>/<id:\d+>' => '<module>/default/view',
'<module>/edit/<id:\d+>' => '<module>/default/edit',
'<module>/delete/<id:\d+>' => '<module>/default/delete',
'<module>/<action>' => '<module>/default/<action>',
'<module>/<controller>' => '<module>/<controller>/index',
'<module>/<controller>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module>/<controller>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module>/<controller>/<id:\d+>' => '<module>/<controller>/view',
'<module>/<controller>/<action>' => '<module>/<controller>/<action>',
],
],
Request Url http://yii2/post
To PostController/actionIndex()
I fix routing, It work.The controllers are checked first, then the modules.
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action>'=>'<controller>/<action>',
'<module:\w+>' => '<module>/default/index',
'<module:\w+>/<id:\d+>' => '<module>/default/view',
'<module:\w+>/edit/<id:\d+>' => '<module>/default/edit',
'<module:\w+>/delete/<id:\d+>' => '<module>/default/delete',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
'<module:\w+>/<controller:\w+>' => '<module>/<controller>/index',
'<module:\w+>/<controller:\w+>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module:\w+>/<controller:\w+>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
'<module:\w+>/<controller:\w+>/<action>' => '<module>/<controller>/<action>',

Yii2 captcha always incorrect

Whan I add 'on' => 'create' in my rule my captcha always incorrect
Off that work good why?
I try to let it to other controller support/captcha but still incorrect
MyView
<?= Captcha::widget([
'id' => 'captcha',
'model' => $model,
'attribute' => 'verifyCode',
'options' => ['class' => 'form-control',
'data-v-rule' => '',
'captchaAction' => 'support/captcha',
'data-v-msg' => $i18n->t('please.input.verify.code'),
'placeholder' => $i18n->t('please.input.verify.code', '', false)]
]); ?>
Myrule
...
[['verifyCode'], 'captcha', 'message' => $i18n->t('error.verifyCode'),'captchaAction' => 'support/captcha' , 'on' => 'create'],
...
I get seesion
$_SESSION = [
'__flash' => [],
'__captcha/site/captcha' => 'nnbioo',
'__captcha/site/captchacount' => 1,
'__captcha/support/captcha' => 'cacijq',
'__captcha/support/captchacount' => 1,
];
Mycontroller
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => CaptchaAction::className(),
],
];
}
$customerServiceModel->load($post) is not working, because attribute verifyCode is not safe in this case. Your rule is specified with 'on' => 'create' - so it means, it's save on scenario create. You didn't assigned this scenario to the model so there's 2 solutions:
Remove 'on' => 'create' from rule
Assign create scenario to model by $customerServiceModel->scenario = 'create'; before using load().

How i can force an yii2 module to use a specific connection for all his models?

on a module I have add a component named db where i put, like the main Yii component, the data for database connection, I need in my module use everytime the db specified in his configuration for all models and not the main database connection, how I can do this?
You have several way eg. using a separated configuration in app/config/main.php
eg adding a specific dbMyMod to component config
return [
// ...
'components' => [
// ...
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=example',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'dbMyMod ' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=hostForMudle;dbname=module_db_name',
'username' => 'user_module_name',
'password' => 'password',
'charset' => 'utf8',
],
],
or one way that not require a static configuration in app/confing
could be based on a module function that return a proper db connection
public function myModuleDbCon()
{
$myDbCon = new yii\db\Connection([
'dsn' => 'mysql:host=localhost;dbname=example',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
]);
return myDbConn;
}
then in you module you can retrive the module db connection
aDbConn = Yii::$app->getModule('my_module_name')->myModuleClass->myModuleDbCon();
.
$command = $aDbConn->createCommand('SELECT * FROM myTable');
$result= $command->queryAll();

How to debug flysystem

I install Yii 2 File kit extendsion, and set local file storage success.
Then i config SFTP then run command
Yii::$app->fileStorage1->getFilesystem()->createDir('demo')
So it not work. and only show result false.
So i don't know what the error.
I try conffig:
'fileStorage1' => [
'class' => '\trntv\filekit\Storage',
//'baseUrl' => '#storageUrl1',
//'filesystemComponent' => 'fs'
'filesystemComponent' => 'sftpFs'
],
'sftpFs' => [
'class' => 'creocoder\flysystem\SftpFilesystem',
'host' => 'Myserver',
'port' => 22,
'username' => 'u',
'password' => 'p',
//'privateKey' => '/path/to/or/contents/of/privatekey',
// 'timeout' => 60,
'root' => 'public_html',
// 'permPrivate' => 0700,
// 'permPublic' => 0744,
],

Could not send email: unknown Cake\Network\Exception\SocketException in cakephp3

Okay I have been to almost all threads but I couldn't find the answer what I want.
Controller: RegistersController
namespace App\Controller;
use App\Controller\AppController;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Event\Event;
use Cake\Mailer\Email;
use Cake\Routing\Router;
class RegistersController extends AppController
{
public function add()
{
$this->loadModel('Tbusers');
$tbusers = $this->Tbusers->newEntity();
if ($this->request->is('post'))
{
$tbusers = $this->Tbusers->patchEntity($tbusers, $this->request->data);
if ($this->Tbusers->save($tbusers)) {
$this->Flash->success(__('Yayie! You have been registered.'));
$baseurl = Router::url('/', true);
$email = base64_encode($this->request->data['email']);
$first_name = $this->request->data['fname'];
$last_name = $this->request->data['lname'];
$username = $first_name.' '.$last_name;
//$confirmation_link = $baseurl.'users/confirm/'.$email;
$email = new Email();
$email->template('email')
->subject('Student Portal - Signup')
->emailFormat('html')
->to($this->request->data['email'])
//->viewVars(['confirmation_link' => $confirmation_link,'username'=>$username])
->viewVars(['username'=>$username])
->from('dotnetdev555#gmail.com')
->send();
if ($email->send())
{
// Success
echo "mail sent";
}
else
{
echo "Mail not sent";
// Failure
}
return $this->redirect(['action' => 'add']);
}
}
Inside Template->Email->html I have file called
email.ctp
Hello <?php echo $username; ?>,<br/>
Thanks,<br/>
Jaymin Sejpal Founder at Student Portal
app.php
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'smtp.elasticemail.com',
'port' => 2525,
'timeout' => 30,
'username' => 'dotnetdev555#gmail.com',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],
I don't know what I am missing to it. I am refferring this
http://book.cakephp.org/3.0/en/core-libraries/email.html for now.
The email host that you are using might be using smtp protocol , so in your app.php . Try editing you className parameter to 'Smtp'
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'smtp.elasticemail.com',
'port' => 2525,
'timeout' => 30,
'username' => 'dotnetdev555#gmail.com',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],