laravel 8 Sending smtp email using gmail fails with current settings - php

I want to send email using smtp with my laravel 8 project. I have an example project in which this works. Using this configuration in my current project is unsuccessful.
I am unable to use the example 1 one 1 because the folderstructure is different. My example uses the default laravel folder structure, my current project has a modular layout in which each folder represents an entity that has its own controllers, models etc. I have been on it for a while and would appreciate outside advice in order to get it working.
The 'allow less secure apps' option is enabled in gmail.
The content of my test_mail.blade.php file is just some basic html.
Mail function is called from inside update function for convenience.
Frontend::email.test_mail , this points to a view that is in the same entity folder.
The example uses: /folder/subfolder/view, the rest of it is the same as the example.
$data = array("name"=>"testname", "employee_nr"=>"010" );
$subject = 'test_message';
$to_email = 'realtargetaddress#gmail.com';
Mail::send('Frontend::email.test_mail',$data, function($message) use( $to_email,$subject)
{
$message->to($to_email)->subject($subject);
$message->from($to_email,$subject);
});
configuration of .env file. (exact copy from example)
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465 # 587 for tls - 456 for ssl
MAIL_FROM_ADDRESS=some_address#gmail.com
MAIL_USERNAME=some_address#gmail.com
MAIL_PASSWORD=**********
MAIL_ENCRYPTION=ssl # tls or ssl
MAIL_FROM_NAME="${APP_NAME}"
config/mail.php (also a exact copy from my example)
'driver' => env('MAIL_MAILER', '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'),
'sendmail' => '/usr/sbin/sendmail -bs',
'from' => [
'address' => env('MAIL_FROM_ADDRESS',
'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
Mail.php extends Facade
This class is in the Illuminate\Support\Facades namespace.
My example uses mail.manager in the getFacadeAccessor() method, which fails in my current project.
Using mailer instead of mail.manager does not produce errors but also does not work.
console commands that used after making changes to .env
php artisan config:clear
php artisan config:cache
php artisan cache:clear
edit The error that this gives me is:
Unable to resolve NULL driver for [Illuminate\Mail\TransportManager].
I searched with that but did not find a fix.
solution I was adviced to add the following code to mail.php, make sure to add it on top and to run the console commands. After i did this the mail function worked for me.
'mailers' => [
'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,
],
],

Related

Email works on Local Environment, but not Production Environment. Not getting any errors

I have just moved my laravel 5.5 website to a server (digitalocean, debian9). For some reason emails wont get sent from my server and I'm not getting any errors.
.env:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAILGUN_DOMAIN=MYDOMAIN
MAILGUN_SECRET=key-SECRET_KEY
MAIL_USERNAME=MYUSERNAME
MAIL_PASSWORD=MYPASSWORD
MAIL_ENCRYPTION=tls
config/mail.php:
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'MY_EMAIL'),
'name' => env('MAIL_FROM_NAME', 'NAME'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
config/services.php:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
send mail controller:
public function sendMail( $id, Request $request )
{
$user = User::find($id);
$data = [
'subject' => $request->subject,
'bodyMessage' => $request->message,
'image' => $request->image,
];
Mail::to($user->email)
->send(new test($data));
Session::flash('email', 'Message !');
return back();
}
I looked through my UFW firewall and i couldn't see anything related to mail getting blocked.
My email Service (mailgun) doesn't output anything in the logs when i try to send a mail. So I guess its not even reaching them.
I have no idea what to do at this point. I don't even know where I should look for the problem, is it a server problem? laravel problem? firewall problem?...
Would really appreciate some help!
Well, you can check several things :
• Is your production .env correct ?
• Have you make a composer-install recently ?
• Are you sure the php versions of your dev and prod environnement are the same ? because Swift Mailer requires PHP 7.0 or higher (proc_* functions must be available).
I have no idea why it didn't work, feels like i tried everything. Ended up using PHPMailer instead, which worked fine.

Laravel 5.1 - Swift_TransportException - cant send emails

at .env file I have this code:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=azure_edhjjjjj67781b06b18228961b#azure.com
MAIL_PASSWORD=hggahsd666
MAIL_ENCRYPTION=tls
and I get this screen:
I also try:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=465
MAIL_USERNAME=azure_edhjjjjj67781b06b18228961b#azure.com
MAIL_PASSWORD=hggahsd666
MAIL_ENCRYPTION=ssl
but dont works again...
I also try to change settings into config/mail.php to be the same as on .env file but dont work and at the end I try command
php artisan cache:clear
Is possible problem cause redirection to secure domain https://asdasd.com/sendEmail ?
keep mail.php like this way
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => 'hello#example.com',
'name' => 'Example',
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
Then cross check all your credentials in .env file, If still not work then go to your email account and change app settings to allow less secure apps

Laravel 5.4 SMTP error "send AUTH command first"

I'm getting the following error trying to send mail from localhost using smtp:
Expected response code 250 but got code "503", with message "503 5.5.4
Error: send AUTH command first. "
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.yandex.com
MAIL_PORT=465
MAIL_USERNAME=robot#domain.com
MAIL_PASSWORD=11111111
MAIL_ENCRYPTION=ssl
MAIL_FROM=robot#domain.com
MAIL_NAME=MY.NAME
config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.yandex.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => 'robot#domain.com',
'name' => 'MY.NAME',
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('robot#domain.com'),
'password' => env('11111111'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
Tried: changing ports, encryption, clearing cache, restarting server in all possible combinations. :)
As I see it there's one more parameter I need to pass to the mailer library. Some thing like
auth_mode=login_first
Can this be done through laravel settings?
I'm posting my working settings. You've got to check how laravel env helper function is used in your config file. Also when using smtp.yandex.com auth email and form email must match.
Laravel Docs for env()
The env function gets the value of an environment variable or returns a default value:
$env = env('APP_ENV');
// Return a default value if the variable doesn't exist...
$env = env('APP_ENV', 'production');
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.yandex.com
MAIL_PORT=465
MAIL_USERNAME=robot#mydomain.com
MAIL_PASSWORD=123123123
MAIL_ENCRYPTION=ssl
MAIL_FROM=robot#mydomain.com
MAIL_NAME=MY.NAME
config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.yandex.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM','robot#mydomain.com'),
'name' => env('MAIL_NAME','MY.NAME'),
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME','robot#mydomain.com'),
'password' => env('MAIL_PASSWORD','123123123'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
Controller function
public function testmail()
{
$user = Auth::user();
$pathToLogo = config('app.url').'/images/logo/logo_250.png';
Mail::send('emails.testmail', array('user' => $user, 'pathToLogo' => $pathToLogo), function($message) use ($user)
{
$message->to($user->email);
$message->subject('Test message');
});
return redirect()->route('home')->with('message','Test message sent.');
}

Error when try to use mandrill for password resetting on Laravel 5

I try to create a password resetting system on laravel 5 and this what I do:
1. Route.php
Route::get('/password/email','Auth\PasswordController#getEmail');
Route::post('/password/email','Auth\PasswordController#postEmail');
Route::get('/password/reset/{token}','Auth\PasswordController#getReset');
Route::post('/password/reset','Auth\PasswordController#postReset');
2. .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mandrillapp.com
MAIL_PORT=587
MAIL_USERNAME=*****#gmail.com
MAIL_PASSWORD=********
3. composer.json
...
"require": {
....
....
"guzzlehttp/guzzle": "~5.0"
},
...
4. mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mandrillapp.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => "****#gmail.com", 'name' => "****"],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
when i try it i get a success message on the page but i don't receive any email.
Try use mandrill driver
Add your key in config/services.php and change your mail driver in .env to mandrill
See it in: https://laravel.com/docs/5.2/mail

Laravel 5.1 Mail::send how to know why it don't sends mails?

I am trying Laravel do a mail send. When I execute the code, nothing happens, no errors, no logs, no returning mails, anything.
Config Env
MAIL_DRIVER=smtp
MAIL_HOST=mail.domain.es
MAIL_PORT=587
MAIL_USERNAME=noreply#domain.es
MAIL_PASSWORD=xxxxxx
MAIL_FROM=noreply#domain.es
MAIL_NAME=Domain Name
MAIL_ENCRYPTION=null
Mail.php
return [
'driver' => 'smtp',
'host' => env('MAIL_HOST', 'mail.domain.es'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'noreply#domain.es', 'name' => 'Domain Name'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', 'noreply#domain.es'),
'password' => env('MAIL_PASSWORD', 'xxxxx'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => env('MAIL_PRETEND', true),
];
Code in the controller
$accion = Accion::findOrFail($id);
Mail::send('emails.notificar', ['accion' => $accion], function ($m) use ($accion) {
$m->from(env('MAIL_FROM'), env('MAIL_NAME'));
$m->to("jtd#adagal.es", "Jtd")->subject('Nova acción formativa');
});
Did you see any error? I did everything that is marked in the official docs but still no response, no mail, no error.
pretend' => env('MAIL_PRETEND', true) change this to false
When the mailer is in pretend mode, messages will be written to your application's log files instead of being sent to the recipient.

Categories