Orie Chinedu Emmanuel
3 min readSep 11, 2017

An Escape Route to Problems Sending Email Using Gmail SMTP in PHP/Laravel

Have you found yourself in such situation whereby you have done all you thought was needful to get the PHPMailer send your email yet it doesn’t work. You’re pretty sure you’ve done the configuration settings to the letters yet it only ends up in returning error messages. In my own case, I had to compare my settings with my friend whose own is working perfectly, guess what! Everything is the same, yet mine doesn’t send. Such experience could be really frustrating. This article is here to tell you to cry no more, you can do it. All you need is one more try applying the information I am about to pass here.

I will also be explaining how to handle it both when working with Vanilla PHP and when working with Laravel.

I assume you already understand the nitty-gritty of sending mail using PHPMailer. So I’ll not be touching everything that concerns handling mails with PHPMailer. Our concern is to crack what normally seems to be a hard nut whereby the email sending keeps failing even when everything seems to have been done rightly

Working with Vanilla PHP:

The first step to sending mail with PHPMailer is to require PHPMailerAutoload class.

require ‘…/PHMailer/PHPMailerAutoload.php’;

Having loaded the necessary classes , next is to instantiate the PHPMailer class and start sending your mails.

$mail = new PHPMailer;$mail->Mailer = ‘smtp’;$mail->IsSMTP(); // enable SMTP$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘smtp.gmail.com’;
$mail->SMTPOptions = array( ‘ssl’ => array( ‘verify_peer’ => false, ‘verify_peer_name’ => false, ‘allow_self_signed’ => true ));
$mail->Port = 465; ));$mail->IsHTML(true);$mail->Username = “youremail@gmail.com”;$mail->Password = “youremailpassword”;

It’s done! With the settings above in place, you can send your email without any trouble. Note the SMTPOptions is the essence of this article, without it in place; you’d continue to encounter errors trying to send your emails. This kept me for days before I discovered it.

Working with Laravel:

Like I said earlier, this article assumes the reader already understands how to use Laravel Mailable class. The settings are done in the .env file . Open the .env file and do the following:

MAIL_DRIVER=smtpMAIL_HOST=smtp.gmail.comMAIL_PORT=465MAIL_USERNAME=youremail@gmail.comMAIL_PASSWORD=youremailpasswordMAIL_ENCRYPTION=ssl

If after doing all that is necessary you are getting the kind of error shown below:

(1/1) Swift_TransportExceptionConnection could    not be established with host smtp.gmail.com [ #0]in StreamBuffer.php (line   270)at Swift_Transport_StreamBuffer->_establishSocketConnection()in StreamBuffer.php (line 62)at Swift_Transport_StreamBuffer->initialize(array('protocol' => 'ssl', 'host'   => 'smtp.gmail.com', 'port' => 465, 'timeout' => 30, 'blocking'   => 1, 'tls' => false, 'type' => 1,   'stream_context_options' => array()))in AbstractSmtpTransport.php (line 113)

Do not panic. All you need do is follow the steps below and it’ll be over. I promise you.

· Go the Vendor/swiftmailer/lib/Transport/StreamBuffer.php.

· Locate line 236

· Add the following lines of code after line 263

$options['ssl']['verify_peer'] = FALSE;$options['ssl']['verify_peer_name'] = FALSE;

Save it and try sending your mail again. Now you can observe your mail was successfully sent. That’s it. Nothing more.

Thanks for reading through. If you found this post helpful, kindly give a clap using the clap button below and don’t forget to share to others who may need it.