Configure Amazon Linux server to send SMTP emails using gmail

1) Make sure your hostname reflects your public dns record

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-hostname.html

We edited our DNS to have a public dns of "webserver.mydomain.com"

So, we edited key file:

sudo nano /etc/sysconfig/network

HOSTNAME=webserver.mydomain.com

and did a "sudo reboot"

2) Install SSMTP

sudo yum install ssmtp

3) Configure ssmtp.conf

a) sudo nano /etc/ssmtp/ssmtp.conf

b) Change:

mailhub=smtp.gmail.com:587

UseSTARTTLS=Yes

AuthUser=yourname@gmail.com

AuthPass=yourpassword

4) If you have problems, turn on debug mode

In ssmtp.conf, put first line: Debug=Yes

It will log everything to:

sudo nano /var/log/maillog

5) Edit ./etc/ssmtp/revaliases

root:Devin Johnson<myname@myemail.com>:smtp.gmail.com:587

6) Edit php.ini

a) sudo nano /etc/.php.ini

b) Search on "mail function"

c) Change sendmail_path to point to ssmtp

sendmail_path = /usr/sbin/ssmtp -t -f 'djohnson@mydomain.com'

6) If you can send emails, but the name of the "from" email is Apache, add this to your PHP:

$headers = 'From: Devin Johnson<djohnson@stockmarketmba.com>' . "\r\n" .

'Reply-To: djohnson@stockmarketmba.com' . "\r\n" .

'X-Mailer: PHP/' . phpversion();


mail($email,$subject,$strBody, $headers);