Profile pic of Tommy KuTommy Ku's Blog

Reading and thinking

Setup Gmail SMTP on Openshift hosted app

Posted on July 26, 2014

Sending email on the PasS Openshift may be a bit troublesome because you cannot install sendmail. By default, mail function of php is not installed as well. You will need to connect to external POP/IMAP/SMTP servers to send and receive email on Openshift. In this post I am showing you how I send verificaiton email from Openshift.

It happens that a project I am working on demands to send verification email upon user registration. At first I tried SendGrid with no avail because their terms restrict email types to

where verification email is not in the list.

The silver lining

For someone like me who do want to send email but does not have a SMTP server, remember Google. For one Gmail account you can send up to 99 email via Google’s SMTP server every day, and you can have as many Gmail account as you want.

Openshift opened the ports we use to send/receive email, too!

Whatever library you use, the protocol is standard, in my project I chose Swift Mailer.

Show me the code


// ...

// make sure you get these SMTP settings right
$transport = Swift_SmtpTransport::newInstance('', 465, "ssl") 
    ->setUsername('[email protected]')

$mailer = Swift_Mailer::newInstance($transport);
// the message itself
$message = Swift_Message::newInstance('email subject')
    ->setFrom(array('[email protected]' => 'no reply'))
    ->setTo(array('[email protected]'))
    ->setBody("email body");

$result = $mailer->send($message);

// ...

You are not restricted to using only Gmail’s server, if you own or have rent a mail server supporting these protocols, it is not hard to set it up to use your own mail server.

You could also look at...