How to set mail send timeout? - yii2

So far here is my config in common/config/main-local.php
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'in-v3.mailjet.com',
'username' => 'myUsername',
'password' => 'myPass',
'port' => '465',
'encryption' => 'ssl',
],
How I can set timeout limit while sending an email?
I have no reference to set mail send timeout in Yii2.
Thanks in advance.

'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'in-v3.mailjet.com',
'username' => 'myUsername',
'password' => 'myPass',
'port' => '465',
'encryption' => 'ssl',
'timeout' => 2000 //in second
],
it's on vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php::setTimeout() you can check the value by
\yii\helpers\VarDumper::dump(Yii::$app->mailer->transport->getTimeout(),10,1);

Related

SwiftMailer - Yii2 can't send email

I have problem using swiftmailer with yii2.
'components' => [
'mailer' => [
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.mandrillapp.com',
'port' => 587,
'username' => 'noreply#mysociete.com',
'password' => 'MY API KEY HERE',
],
],
],
to send after user registration i use the code below :
$isSuccessfullySaved = $leadModel->save();
if (!$isSuccessfullySaved) {
$response = $this->asJson(['errors' => ['webservice' => 'Error while saving the lead']]);
$response->statusCode = 550;
return $response->send();
}
$lastStep = Json::decode(file_get_contents(__DIR__ . '/../config-offers/offers.json'));
$isSuccessfullySent = Yii::$app->mailer->compose(
'offer-summary',
ArrayHelper::merge($leadModel->getAttributes(), $lastStep['offres'][$bestOffer])
)
->setFrom(['noreply#mysociete.com' => 'My Societe'])
->setTo($leadModel->emailAddress)
->setSubject('mysociete.com- Votre offre')
->send();
if (!$isSuccessfullySent) {
Yii::error("Could not send the email", __METHOD__);
}
What should be wrong. There is no error written in any log.
Have you tried setting the encryption in the transport? I am not sure about Mandrillapp needing it, but you could give it a try:
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.mandrillapp.com',
'port' => 587,
'username' => 'noreply#mysociete.com',
'password' => 'MY API KEY HERE',
'encryption' => 'tls',
],

"Database hosts array is empty." after deploying Laravel project on AWS Elastic Beanstalk

I deployed my project to an AWS EC2 instance using Elastic Beanstalk. I used this tutorial https://www.youtube.com/watch?v=ISVaMijczKc as a reference while deploying. I am following everything as it is in the tutorial but I ended up with an error.
Database hosts array is empty. (SQL: select * from
resource_categories)
The following are my codes.
database.php
<?php
define('RDS_HOSTNAME', $_SERVER['RED_HOSTNAME']);
define('RDS_USERNAME', $_SERVER['RED_USERNAME']);
define('RDS_PASSWORD', $_SERVER['RED_PASSWORD']);
define('RDS_DB_NAME', $_SERVER['RED_DB_NAME']);
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'driver' => 'mysql',
'host' => RDS_HOSTNAME,
'port' => env('DB_PORT', '3306'),
'database' => RDS_DB_NAME,
'username' => RDS_USERNAME,
'password' => RDS_PASSWORD,
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
'migrations' => 'migrations',
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
];
.ENV
APP_NAME="MyProject"
APP_ENV=development
APP_KEY=base64:FlVBd61BUEzVx6ACa6OOn3Jrp4z+VRpug+F1K1ZeJOs=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myDB
DB_USERNAME=root
DB_PASSWORD=
I've gone through a similar problem and MarkB helped me. There's a specific procedure you must follow when you SSH into the instance.
If you run
export
you can see that there's no variable named RDS_HOSTNAME in that Linux shell and that's why you're getting that error.
If you run
/opt/elasticbeanstalk/bin/get-config environment
you can see an object with the list of properties, including that RDS_HOSTNAME.
If you run
/opt/elasticbeanstalk/bin/get-config environment -k RDS_USERNAME
you get the value associated with that particular property. This value needs to be saved in a variable and exported so that other commands can recognize it.
If you run
export RDS_USERNAME="value"
then when you run
export
you can see that this is now available.
Now if you run the command you wanted it's likely gonna work (you might need to repeat this for RDS_USERNAME, RDS_PASSWORD and RDS_DB_NAME).
Note: if that didn't work, then your problem might be similar to this one.
This is a bug report which might be related to this problem. The long and the short is that if the ConnectionFactory gets passed a NULL value it will (erroneously) trigger that error. I suspect that all of your RDS variables are empty.
To set these values, you can follow instructions here.

Yii2 smtp email is not working on godaddy

I have deploy a Yii2 website from one server to godaddy. Its showing an error :-
'If you receive error " Connection could not be established with host smtp.gmail.com [Connection refused #111] "'
I have set the mail configs below:-
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 't********m#gmail.com',
'password' => '***************',
'port' => '465',
'encryption' => 'tls', //depends if you need it
],
],
I resolved it by just commenting the transport tag as below:-
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
//'transport' => [
// 'class' => 'Swift_SmtpTransport',
// 'host' => 'smtp.gmail.com',
// 'username' => 't********m#gmail.com',
// 'password' => '***************',
// 'port' => '465',
// 'encryption' => 'tls', //depends if you need it
// ],
],

Yii2 Email How to set sender name

i using Mailer to send email, so i have problem about sender name
This is my config
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'messageConfig' => [
'charset' => 'UTF-8',
'from' => ['admin#app.com' => 'App Sender Name'],
],
'transport' => [
'class' => 'Swift_MailTransport',
],
],
So it's not work. I goto inbox and only email showed.
And i need show as
Example:
it's work when i update setFrom() method.
Ex: $mailer->setFrom(['email#app.com' => 'App Name']). And here is config Yii2 for send by PHP mail and with sender name
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'messageConfig' => [
'charset' => 'UTF-8',
'from' => ['admin#app.com' => 'App Sender Name'],
],
'transport' => [
'class' => 'Swift_MailTransport',
],
],
and send email
Yii::$app->mailer->compose()
->setTo('client#email.com')
->setFrom(['admin#app.com' => 'App Name'])
....
Every component and sub-component in yii2 is configurable by dependency injection, this case is same, eg:
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'noreply.yourcompany#gmail.com',
'password' => '123456',
'port' => '465',
'encryption' => 'ssl',
],

Send email from other smtp in yii2

I use swiftmailer to send email from yii2. I get code from websites, but most of them send email from gmail.com. I want to send email from my university email, but it's not working. What's the problem. This is the code:
In main-local.php
'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*/false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.students.del.ac.id',
'username' => 'if414024#students.del.ac.id',
'password' => 'bmelar28',
'port' => '587',
'encryption' => 'tls',
],
],
Remove smtp in 'host' => students.del.ac.id
show below code
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'students.del.ac.id',
'username' => 'if414024#students.del.ac.id',
'password' => 'bmelar28',
'port' => '587',
'encryption' => 'tls',
]
]
Try this : change port number and host
'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*/false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'students.del.ac.id',
'username' => 'if414024#students.del.ac.id',
'password' => 'bmelar28',
'port' => '25',
'encryption' => 'tls',
],
],