Sending emails using Amazon SES, the SMTP way

I tried to send emails from php using Amazon SES from my Amazon EC2 instance, but took me some time to get it right. This blog is intended to save that time for some other poor fellow. So here it goes.

First of all go ahead and verify an email address from which you’ll be sending emails from. You can follow the steps here : http://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-email-addresses.html

Then obtain your SMTP credentials from AWS, mind you the SMTP credentials are different from your AWS login credentials. You can follow the steps here : http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html

Let’s install Postfix now. Postfix is a MTA (Mail Tranfer Agent) which sends your mails for you to the address you specify in say the $to parameter in say the mail() function in php or, if you configure it properly , to Amazon SES so that it can send it on your behalf. Install postfix simply doing

sudo apt-get install postfix

Install stunnel

sudo apt-get install stunnel4

Configure secure tunnel using stunnel following the instructions here : http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp-secure-tunnel.html The command mentioned on this page to start stunnel will give you an error so use this instead

sudo stunnel4 /etc/stunnel/stunnel.conf

Then follow the steps in the ‘Configure the server’ section on this page : http://www.millcreeksys.com/how-to-configure-your-postfix-server-to-relay-email-through-amazon-simple-email-service-ses/

Now you can use the following code to send emails :

$to = ‘someone@domain.com’;
$subject = “Hello”;
$header = “from: webmaster@some.com”;
$message = “Hello World”;
$sentmail = mail($to,$subject,$message,$header);

But this would still send your emails without using Amazon SES. If you read /var/log/mail.log you’ll see that you might be getting 

554 Message rejected: Email address is not verified (in reply to end of DATA command)

in your log file. You need to change your php code to the following to send emails properly from Amazon SES :

function sendEmail($fromName, $fromEmail, $to, $subject, $body) {
$header = ‘From: ‘.$fromName.’<’.$fromEmail.’>’.’\r\n’.
'Return-Path: '.$fromEmail.'\r\n';
mail($to, $subject, $body, $header, ‘-f ‘.$fromEmail);

This should get everything working. If you face any problems, let me know in the comments.

Next Up :

You should look at authenticating your emails with SPF, instructions are given here : http://docs.aws.amazon.com/ses/latest/DeveloperGuide/spf.html and authenticating them using DKIM following instructions here : http://docs.aws.amazon.com/ses/latest/DeveloperGuide/dkim.html Setting up DKIM is pretty simple so let me know if you face any problems in that. I haven’t set up SPF yet.

References :









Connecting to Amazon EC2 behind a proxy

This is more kind of a note to self.

Port 22 which is used for ssh is blocked on my college network, so I needed to tunnel ssh connections over HTTP or HTTPS. For this I did the following things :

  • ssh into the remote EC2 machine (using a proxy free connection).
  • Add
    Port 443
    to /etc/ssh/sshd_config below the line which says Port 22. I’m using 443 instead of some other port because right now I don’t serve my website over https. I might need to change this port to something else once I start accepting https connections too.
  • Restart sshd by running

sudo service ssh restart

  • Now on my machine, install corkscrew by running 

sudo apt-get install corkscrew

  • Edit your ~/.ssh/config file to like the one given below

Host AWS-Proxy
Hostname <Public DNS>
Port 443
User ubuntu
IdentityFile <path to key file>
ProxyCommand /usr/bin/corkscrew 3128 %h %p

Host AWS-Free
Hostname <Public DNS>
Port 22
User ubuntu
IdentityFile <path to key file>

  • Now I can simply connect to the EC2 instance when behind proxy using

ssh AWS-Proxy

  • When on a proxy free connection I simply use

ssh AWS-Free


Troubleshooting :

If the above doesn’t work, first of all verify if you’ve allowed incoming connections to your machine over HTTPS (or the custom port that you’re using). Follow the instructions given here for doing so : http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html#adding-security-group-rule

You might need to change the username in the config file depending on the AMI that’s running. For Amazon Linux, the user name is ec2-user. For RHEL5, the user name is often root but might be ec2-user. For an Ubuntu, AMI the user name is ubuntu. Otherwise, check with your AMI provider.

You might need to change the proxy address and port depending on your scenario.


A Crazy Day (And A Crazier Night) In San Francisco

I had gone to the US recently to attend the Mozilla Summit. It was crazy, my first time in the US and that too in and around San Francisco. It was like living inside a TV Series like Alcatraz

We had thought of staying back another day just to go around SF. So here is what happened :

We left the hotel in Santa Clara by the shuttle, reached the airport, then took the BART, once we emerged out of the BART station we realized for the first time we were in California. I mean Santa Clara was loads of fun but SF had such high rises, it blew my mind. It was like walking in the sets of some TV series. We were headed to the Mozilla SF office. Boy that office is insanely beautiful, with such a spectacular view of the Bay Bridge. Met most of the Devtools Team, again, and roamed around the office letting ourselves free at the mini-bars :) We then decided to take a walk down the bay.

We roamed on the pier and then came back. We decided to walk to Chinatown. In the way, saw buildings, Tesla’s, roamed around Chinatown, shopped a bit. We decided to go to Fisherman’s Wharf. It has a lot of the local food, had burgers at In n Out, had clam chowder. We then went to the DNA lounge, nearly got our asses kicked for photography, went next to ZeitGeist Pub. It had a beer garden, about 50 different kinds of beer. After spending an hour or too there, we decided to leave.

We were having second thoughts about getting accomodation to spend the night, since we wanted to utilize the whole time, so we finally decided not to book anything. Things had pretty much closed by the time we left from Zeitgeist at 2. So we decided to walk all the way down to the Golden Gate Bridge, planning to see the sunrise from there. It was pretty cool and worrying at the same time, walking around town seeing everything so still and calm while also sometimes not feeling so safe and doubting our decision, but we still we walked.

We finally reached the foot of bridge, the Golden Gate park, at around 3:30. It was really dark, no street lights, and cold. Since it was dark, we planned to wait till sunrise. Sunrise was gonna happen at 7:15 so we sat down at the bench on the side of the road like a hobo, shivering in the cold :P It was at this time we cursed our decision to be doing all this. But we finally slept.

We woke up around 6:40, fucked up in cold, but happy there was going to be the Sun, finally. We realized the importance of the Sun at that time. We started finding our way to the bridge which apparently is not so clear after sleeping outside in the cold. After hunting around, we finally reached on the bridge on 7:10 just on time. The city looked spectacular mesmerizing, people jogging around, Alacatraz just bathing in the morning haze and the grand Golden Gate bridge right in front of us. That moment is something I would surely remember for a long time and then the sun rose. The first rays, they felt so beautiful, a ray of light we had actually earned (or that’s what we try and think).

We took our time, clicked photos, enjoyed and then came back to the Airport.

Looking back, it was quite fun and memorable.