Cannot send message without a sender address Issue in 1 Environment - laravel

I am having a problem in Laravel with my mailer. The weird part is the issue only happens in my production environment, it works correctly in development.
I have confirmed the proper environment values are set in production. I have also cleared the configuartion cache.
If I remove the environment values in the "from" line and replace them with hard coded values in production it works. So in development it works correctly with environment values in "from", in production it only works with hard coded value.
Does anyone have any thoughts?
my config/mail.php file is as follows:
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => env('MAIL_FROM_ADDRESS'), 'name' => env('APP_NAME')],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
My .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=XXXXX
MAIL_PASSWORD=XXXXX
MAIL_FROM_ADDRESS=XXXXX#XXXXXX.com

Can you please check if env('APP_NAME') exists? name can be absent according to docs but I'm not sure if you can pass a null value.

Related

Laravel 8: Connection could not be established with host smtp.gmail.com

I'm trying to send a simple email from my laravel 8 app using Google SMTP server, but it doesn't work, I'm getting this error:
Swift_TransportException
Connection could not be established with host smtp.gmail.com :stream_socket_client(): SSL:
or
stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
I found many SO post regarding this issue but none of them worked for me, here's a list of what I have tried:
Enabling less secure apps in the google account
using ports 465 and 587
Using smtp.googlemail.com and smtp.gmail.com
Using MAIL_ENCRYPTION=tls and ssl
Changing 'stmp' to 'sendmail'
Blocking switmailer's ssl constraint
Is there something else I can try?
Here's my mail configuration, although I've tried many versions of it:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=address#gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
I had the same problem, I just disabled my antivirus (Avast / windows 10) and its work.
Try this and add the extra auth mode env variable
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=direction#gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
MAIL_AUTH_MODE=login
Now go to config/mail.php
and change
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
to
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => env('MAIL_AUTH_MODE'),
],
Finally, it was a problem with Windows. I found some posts pointing that windows just can't execute some mail services. I tried the exact same code in a computer with Ubuntu and it worked at first try.

Laravel Mail:queue not taking value from env

Not taking value of env MAIL_HOST while sending email
Please describe why this happen ?
try {
Mail::to([
[
'email' => 'abcdef#gmail.com',
'name' => 'ABCD',
],
])->queue(new SendHTMLEmail([
'subject' => "For A Purpose",
'body' => '',
]));
} catch (\Exception $e) { }
When using a queue worker, make sure that you do the following two things whenever you update your application configuration (.env included):
Restart your queue worker(s). Code changes, and also configuration changes, are only picked up by the queue workers when they are restarted.
If you are running in production or, for some other reason, you have your configuration cached (with php artisan config:cache), then you'll also have to re-cache the configuration with the same command before restarting your queue worker(s).
This should be available in your .env
MAIL_DRIVER=smtp
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=
config/mail.php
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
These two files make sure that you have entered credentionals.
Also run,
php artisan config:cache
php artisan queue:restart

Unable to connect with TLS encryption with Laravel 5.5 Swiftmailer

I am trying to send a mail using the Laravel inbuilt swift mailer but I keep getting the "Unable to connect with TLS encryption" error everytime.
Below is my .env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=myemailaddress
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
My config/mail.php file is below:
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.office365.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'myemailaddress'),
'name' => env('MAIL_FROM_NAME', ''),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME','myemailaddress'),
'password' => env('MAIL_PASSWORD','mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
My controller where I initiate the mail sending is below:
if ($loan->save()) {
$name = staff::select('staff_name')->where('staff_id','=',$id)->first();
$data = array(
'Name' => $name,
'Amount' => $request->input('amount'),
'Tenor' => $request->input('tenor'),
'Purpose' => $request->input('purpose')
);
Mail::send('emails.loanrequest', $data, function($message){
$message->from('myaddress');
$message->to('myemailaddress');
$message->subject('New Loan Request');
});
}
Any help that would make this work would be appreciated. Thanks..
I had a similar problem trying to send an email using Laravel. Finally, I discovered that the problem was my antivirus (Avast in my case) software, blocking TLS calls for me. I write this because maybe it could be helpful to someone.
Apparently, It was my proxy server that was blocking my connection. Once I switched my connection, it worked..

Laravel on Namecheap shared hosting, Mailtrap not working

Swift_TransportException in StreamBuffer.php line 265: Connection could not be established with host mailtrap.io [Connection timed out #110]
As far as I can tell this is the exact same issue as this question here:Laravel 5, Password reset email not working
I'm not using mail settings from .env (everything is commented out), so everything is configured in config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'mail'),
'host' => env('MAIL_HOST', 'mailtrap.io'),
'port' => env('MAIL_PORT', 25),
'from' => [
'address' => 'no-reply#mydomain.com',
'name' => 'mydomain.com',
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', [username]),
'password' => env('MAIL_PASSWORD', [password]),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
With these settings, I do not get the above error, but mail is not being delivered to the mailtrap mailbox. Locally, mail is working with the following config/mail.php:
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailtrap.io'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => 'noreply#mydomain.com',
'name' => 'mydomain.com',
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', [username]),
'password' => env('MAIL_PASSWORD', [password]),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
I have tried with/without tls, ports 465 and 2525, and changing the mail driver from smtp to mail, but the first config/mail.php settings are the only ones I have found that do not produce an error.
Any help greatly appreciated, thanks for reading!
If you're using your gmail account to send emails, this might be happening because google finds it suspicious.
Try allowing your app from here.
Log in to your gmail account and go the the security settings and let google know that it was indeed you who was trying to access your account.
They put a block on sending emails like this because it looked
suspicious.
Pekka's comment was spot on. Just a matter of opening ports. Doh!

Laravel: send password reset link

Problem. When I try to send password reset link with Laravel (v.5.2), i get this error message:
Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 220 but got code "", with message ""
How can I fix this?
.env.
MAIL_DRIVER=smtp
MAIL_HOST=send.one.com
MAIL_PORT=465
MAIL_USERNAME=info#myemail.se
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null
mail.php
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'send.one.com'),
'port' => env('MAIL_PORT', 465),
'from' => ['address' => 'info#myemail.se', 'name' => 'donotreply'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
Other info. I have my project on a shared host site called one.com. The project is quite small, and not much is changed from the Laravel installation. I uploaded it by copying it into the domain, and changed a few settings.
It could be many settings, my current suspicion would be the MAIL_ENCRYPTION=null. Have you tried setting it to MAIL_ENCRYPTION=ssl? Since it's port 465, it might be neither null or tls (the env() fallback) .

Resources