Profile pic of Tommy KuTommy Ku's Blog

Reading and thinking

Setup Gmail SMTP on Openshift hosted Laravel app

Posted on  by Tommy Ku

This post is more than 2 years old, it may contain outdated information

edit: This guide was written for Laravel 4.2. If you are using Laravel 5.0, similar settings are done separately in your environment configuration file .env, or in config/mail.php directly

This guide simply talk about the setup to send email in Laravel on Openshift similar as a previous post.

It has been a while after my previous post because I have been struggling to get 2 projects online before the end of 2014. As a student I feel freelance projects overwhelming but extremely rewarding forcing myself to learn and create product valued by my clients.

Talk less and do more, see the settings!

Show me the code

Laravel have the process streamlined, all you need to do to configure it, then you can send email using the Mail class. Below is the only configuration file you need to mess with.

<?php
/* app/config/mail.php */
return array(
	'driver' => 'smtp',
	'host' => 'smtp.gmail.com',
	'port' => 587,
	'from' => array('address' => "{your email address}", 'name' => "{your name}"),
	'encryption' => 'tls',
	'username' => "{your email address}",
	'password' => "{your gmail password}",
	'sendmail' => '/usr/sbin/sendmail -bs',
	'pretend' => false,
);

Mail class provides an extensive interface for sending emails. To send an email you can do something like:

/* freshly copied from laravel.com */
Mail::send('emails.welcome', $data, function($message)
{
  $message->from('[email protected]', 'Laravel');

  $message->to('[email protected]')->cc('[email protected]');

  $message->attach($pathToFile);
});

The same could be done for other SMTP services you currently use. You are done here if you are not using Gmail, and please continue to read the next section if you do use Gmail.

Working with Gmail (Important)

You may have noticed that even with the settings right, it is still not working. On this STMP matter Google has introduced a security policy “Less Secure Apps”.

If the first email sent via Laravel is not reaching its destination, logon to Gmail and you are probably seeing a email like this:


Img. Good job, Google

and inside it says


Img. Google doesn’t trust your app

Google is not confident in your Laravel app but clearly you are. What you need to do is to follow that link and allow for Google sign in with “less secure apps”.

References

1. Laravel - The PHP Framework For Web Artisans. (Mail) 2. Accessing Gmail in Your Email Program or Mobile Device - About Email

You could also look at...

This post first appeared on . Permalink:

About the author

Profile pic of Tommy Ku

Tommy Ku, a Hong Kong-based Software Engineer experienced developing PHP and Java-based web solutions and passionate in Web technology.

Also a hobbyist digital and film photographer.