Laravel 5.2 - Mail randomly got exception time out - php

I'm using PHP 5.6 and Laravel 5.2
My email is running in cron queues. But randomly got the exception timeout.
Connection to tcp://mail.example.com:587 Timed Out
Here is my code:
public function sendNotificationByEMail($information){
Mail::send($information['template'], ['param' => $information['param']], function ($message) use ($information) {
$message->from('admin#example.com', $information['mailAlias']);
$message->to($information['mail']);
if(isset($information['cc']) && !empty($information['cc'])){
$message->cc($information['cc']);
}
$message->subject($information['subject']);
});
}
class SendNotificationEmail extends Job implements ShouldQueue
use InteractsWithQueue, SerializesModels;
try {
$modelHelper->sendNotificationByEMail($result);
} catch(Exception $ex) {
$param['push_notification_note'] = $ex->getMessage();
}
And here is my mail config:
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mail.example.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => env('MAIL_FROM'), 'name' => env('MAIL_NAME')],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
Is my code is wrong, or is something wrong with the server?

Related

Unable to use SMTP Gmail server to send email authentications

I am trying to send User email verification. I have updated the env and mail configuration to suit my Google Mail. However, I am encountering a Swift_TransportException (530) error.
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=okaydots#gmail.com
MAIL_PASSWORD=
mail.php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'okaydots#gmail.com'),
'name' => env('MAIL_FROM_NAME', 'Kyle Jeynes'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('okaydots#gmail'),
'password' => env(''),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
];
User.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify' => true]);
Route::get('/home', 'HomeController#index')->name('home');
HomeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('verified');
}
public function index()
{
return view('home');
}
}
This is the exact error message I am receiving:
Expected response code 250 but got code "530", with message "530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/?p=WantAuthError 133sm47765wme.9 - gsmtp "
After googling this error, I was told to Turn on less secure apps. After doing so, I still receive the same error. How can I allow Laravel to send email verification / emails?
Please revert back your mail.php. You only need to change your .env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=okaydots#gmail.com
MAIL_PASSWORD= #use gmail app password not your gmail password
MAIL_ENCRYPTION=tls
After intensive Googling, I found that the mail.php contains the env() method for the configuration, and therefore expects arg 1 to be the configuration name inside the .env file.
I changed my mail.php to this and now it works:
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'okaydots#gmail.com'),
'name' => env('MAIL_FROM_NAME', 'Kyle Jeynes'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'), # Changed
'password' => env('MAIL_PASSWORD'), # Changed
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
];

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.');
}

Authentication errors sending mail in laravel 5.2

I am trying to send a mail using laravel but am getting this error :
"Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required"
This is my mail.php file
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => null, 'name' => null],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
This is where I send my mail from (routes.php)- for testing though.
Route::get('/send', function(){
mail::send('Email.signup', [], function ($message) {
$message->to('mymail#gmail.com', 'example_name');
$message->subject('Welcome!');
$message->from('hello#app.com', 'Your Application');
});
});
Thank you.

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.

Laravel mail configuration if using providers

I want do a helper who send mails, i was start by doing provider and a custom class for my helper.
my provider register function:
public function register()
{
$this->app->bind('mailer.helper', function (Application $app){
return new MailerHelper($app->make('mailer'));
});
}
my helper function:
class MailerHelper implements SelfHandling{
/** #var Mailer $mailer */
protected $mailer;
public function __construct(Mailer $mailer) {
$this->mailer=$mailer;
}
public function sendFromContact(array $date){
$this->mailer->send('email_templates/contact', $date, function (Message $message){
$message->setTo('stroia.laurentiu92#gmail.com');
$message->setFrom('contact#dianabotezan.ro','Contact website Diana Botezan');
$message->setSubject('Forumlar contact');
});
}
}
my configuration from config/mail.php:
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => null, 'name' => null],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('my_gmal#gmail.com'),
'password' => env('mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
]'
and the problem is laravel have a bad configuration, here is my track from what he try to do:
Connection could not be established with host 127.0.0.1 [Connection refused #111]
1. in StreamBuffer.php line 265
2. at Swift_Transport_StreamBuffer->_establishSocketConnection() in StreamBuffer.php line 62
3. at Swift_Transport_StreamBuffer->initialize(array('protocol' => null, 'host' => '127.0.0.1', 'port' => '2525', 'timeout' => '30', 'blocking' => '1', 'tls' => false, 'type' => '1')) in AbstractSmtpTransport.php line 113
here you can observe he have other config then I set it. What i do wrong ?
It looks like you've put your configuration in the wrong place.
Instead of editing the config/mail.php file directly, you should put your mail configuration settings in the .env file at the root of your project.
You can see here in the sample file shipped with laravel.

Categories