Unable to connect with TLS encryption with Laravel 5.5 Swiftmailer - laravel

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..

Related

How to test sendgrid email with mailtrap for testing?

In my Laravel 7.6 app I use sendgrid for email sending with code in control like :
\Mail
::to($newContactUs->author_email)->
send(new SendgridMail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles));
with class in app/Mail/SendgridMail.php :
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Sichikawa\LaravelSendgridDriver\SendGrid;
use App\Settings;
use App\Http\Traits\funcsTrait;
class SendgridMail extends Mailable
{
use Queueable, SerializesModels;
use SendGrid;
use funcsTrait;
private $m_view_name;
private $m_to;
private $m_cc;
private $m_subject;
private $m_additiveVars;
private $m_attachFiles;
public function __construct( $view_name, $to= [], $cc= '', $subject= '', $additiveVars= [], $attachFiles= [] )
{
$this->m_view_name= $view_name;
$this->m_to= $to;
$this->m_cc= $cc;
$this->m_subject= $subject;
$all_emails_copy = \Config::get('app.all_emails_copy');
if ( empty($this->m_cc) and !empty($all_emails_copy)) {
$this->m_cc= $all_emails_copy;
}
$additiveVars['site_home_url'] = \URL::to('/');
$additiveVars['site_name'] = Settings::getValue('site_name');
$additiveVars['noreply_email'] = Settings::getValue('noreply_email');
$additiveVars['support_signature'] = Settings::getValue('support_signature');
$additiveVars['medium_slogan_img_url'] = config('app.url').config('app.medium_slogan_img_url');
$this->m_additiveVars= $additiveVars;
$this->m_attachFiles= $attachFiles;
}
/**
* Build the message.
*
* #return $this
*/
public function build( )
{
$mailObject= $this
->view( $this->m_view_name)
->subject($this->m_subject)
->to([$this->m_to])
->cc([$this->m_cc])
->with( $this->m_additiveVars )
->sendgrid( $this->m_additiveVars );
foreach( $this->m_attachFiles as $next_attach_file) {
if ( file_exists($next_attach_file) ) {
$mailObject->attach($next_attach_file);
}
}
return $mailObject;
}
}
and template resources/views/emails/contact_us_was_sent.blade.php:
...
<div class="wrapper">
#inject('viewFuncs', 'App\library\viewFuncs')
<h4 class="email_title">
Hello, {!! $to_user_name !!} !
</h4>
...
#include( 'emails.app_footer')
#include( 'emails.emails_style')
</div>
and it works for me now, but now with "Multiple Mail Drivers" I added mailtrap to my .env :
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=NNNNNNNN
MAIL_PASSWORD=NNNNNNNN
and I want to use mailtrap while testing the app using the same
template resources/views/emails/contact_us_was_sent.blade.php
and switching from mailtrap to sendgrid as easy as possible.
I tried something like :
\Mail::mailer('smtp')
->to($newContactUs->author_email)
->send( \Mail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles) );
But got error as \Mail does not support templates.
Are there something to use support templates for mail Method? Some wrapper?
Updated:
Priorly I worked with sendgrid and for this in file config/mail.php I
wrote all sendgrid parameters.
Now I want to write 2 emeil servers and fi=or this in .env I wrote:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=mailtrip_id
MAIL_PASSWORD=mailtrip_password
SENDGRID_HOST=smtp.sendgrid.net
SENDGRID_PORT=587
SENDGRID_ENCRYPTION=tls
SENDGRID_USERNAME=sendgrid_user
SENDGRID_PASSWORD=sendgrid_user_password
and I remade config/mail.php (I got a sample from):
<?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', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'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'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'sendgrid' => [
'transport' => 'sendgrid',
'host' => env('SENDGRID_HOST', 'smtp.sendgrid.net'),
'port' => env('SENDGRID_PORT', 587),
'encryption' => env('SENDGRID_ENCRYPTION', 'tls'),
'username' => env('SENDGRID_USERNAME'),
'password' => env('SENDGRID_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
];
I am not sure that this config file is valid? Are mail config parameters are read from mailers array ?
Looks like default(mailtrip) mail config is used always. Is it invalid format ?
In my control Ido:
$email_mode= 'live';
// $email_mode= 'debug';
if( $email_mode== 'debug' ) {
\Log::info( '-10 Send to mailtrap ::' );
\Mail
::mailer('smtp')
->to('myemail#yahoo.com') // DEBUG
->send(new TestEmail); //
\Log::info( '-10 Send to mailtrap AFTER::' );
}
// sendgrid
if( $email_mode== 'live') {
\Log::info( '-11 Send to sendgrid ::' );
\Mail
::mailer('sendgrid')
->to('myemail#yahoo.com') // DEBUG
->send(new SendgridMail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles));
\Log::info( '-11 Send to sendgrid AFTER::' );
}
I check in logs that live flow is run but anyway I got email at mailtrap.
Thanks!
The short answer is you are using \Mail() which is a native php function
see: https://www.php.net/manual/en/function.mail.php and try to embed that in a laravel Mailer.
This is not how you should use it, this is really not advised.
Some more detail:
As you write mailables, already you should consider, the Mailable should not define the driver, so it's not encouraged to name it SendGridMailA.. see it as MailA send with Mail::mailer($mailDriver)
See:https://laravel.com/docs/7.x/mail#writing-mailables
Long answer, see this video, explains how to implement multiple mail drivers:
https://www.youtube.com/watch?v=HCONO0cwsoI
However it looks like you using it as a debug method. This is not why multiple mail drivers where introduced in laravel 7. It's more like you should use a different driver for bulk mails for example and for password resets...
That makes sense..
For debuging, it more usefull to make the default driver, depend on the APP_DEBUG configuration for example, or introduce an own ENV var, to toggle your production app in debug mode...
I found decision with modifying config/mail.php :
<?php
return [
'default' => env('MAIL_MAILER', 'sendgrid'),
'mailers' => [
'mailtrap' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'sendgrid' => [
'transport' => 'smtp',
'host' => env('SENDGRID_HOST', 'smtp.sendgrid.net'),
'port' => env('SENDGRID_PORT', 587),
'encryption' => env('SENDGRID_ENCRYPTION', 'tls'),
'username' => env('SENDGRID_USERNAME'),
'password' => env('SENDGRID_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
and having 2 groups 'mailtrap' and 'sendgrid' in .env I wrote in control :
\Mail
::mailer(true ? 'sendgrid' : 'mailtrap')
->to($newContactUs->author_email)
->send(new SendgridMail(
'emails/contact_us_was_sent',
$newContactUs->author_email, '', $subject,
$additiveVars,
$attachFiles)
);

Swift_TransportException Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted

I'm trying to send email using laravel 5.4 but I'm getting this error.
I try to enable less secure application for my account but nothing seems to work for me. this is my code:
use Mail;
class UserController extends Controller{
public function register(Request $request)
{
$inputs=$request->all();
$data=$inputs;
Mail::send('mail.ajout', $data, function($message) use ($data)
{
$message->from($data['email']);
$message->subject("Nouvelle inscription");
$message->to('myaccount#gmail.com');
});
}
}
my env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_USERNAME= myacount#gmail.com
MAIL_PASSWORD= mypwd
MAIL_ENCRYPTION=tls
mail.php
<?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', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'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'),
],
],
];
any help please ,thanks in advance
Is there space in after .env variable = ? Then just remove it takes space as a character.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_USERNAME=myacount#gmail.com
MAIL_PASSWORD=mypwd
MAIL_ENCRYPTION=tls
After changing .env clear your config and restart your server.
php artisan config:clear
php artisan serve

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!

Email with laravel SMTP of Godaddy account fails?

I am trying to send an email in laravel with godaddy account but I always get an error
Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "501", with message "501 HELO requires valid address"
Below code I used for the sending the email in controller:
$title = 'Test email';
$content = 'Test email from shingora';
Mail::send('emails.send', ['title' => $title, 'content' => $content], function ($message) {
$message->from('robin.shingora#gmail.com', 'Shingora');
$message->subject('Test email');
$message->to('robin.shingora#gmail.com');
});
And in mail.php
return [
'driver' => 'smtp',
'host' => 'smtpout.asia.secureserver.net',
'port' => 25,
'from' => ['address' => 'example#gmail.com', 'name' => 'test'],
'encryption' => 'ssl',
'username' => 'example#gmail.com',
'password' => '********',
'sendmail' => 'D:\sendmail\sendmail -bs',
'pretend' => false,
];
Set all smtp setting in your .env file
i.e
MAIL_DRIVER=smtp
MAIL_HOST=ssl://smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=example#gmail.com
MAIL_PASSWORD=*******
MAIL_ENCRYPTION=null
OR
return [
'driver' => 'smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'from' => ['address' => 'example#gmail.com', 'name' => 'test'],
'encryption' => 'ssl',
'username' => 'example#gmail.com',
'password' => '********',
'sendmail' => 'D:\sendmail\sendmail -bs',
'pretend' => false,
];
Few weeks ago I had same issue and I solved the problem by realising that GoDaddy likes to have your from and reply-to e-mail should belong to your own domain.
And it may be a problem with your hostname, so I've browsed to another SO question and found this:
Configure SwiftMailer "Local Domain" setting easily, in Symfony 2

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