{"id":18387,"date":"2015-03-24T11:22:51","date_gmt":"2015-03-24T05:52:51","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18387"},"modified":"2016-12-19T14:54:53","modified_gmt":"2016-12-19T09:24:53","slug":"configuring-server-to-relay-email-through-amazon-ses","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/configuring-server-to-relay-email-through-amazon-ses\/","title":{"rendered":"Configuring server to relay email through Amazon SES (Simple Email Service)"},"content":{"rendered":"<p><strong>Configuring server to relay email through Amazon SES<\/strong> will provide easy, real-time access to our sending statistics. It has built-in notifications for bounces, complaints, and deliveries which helps us fine-tune our email-sending strategy.<br \/>\n<strong>Postfix<\/strong> is a free and open-source mail transfer agent (MTA) that routes and delivers electronic mail.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\"><strong>Amazon Simple Email Service<\/strong><\/h3>\n<p><\/br>Amazon SES is a cost-effective outbound-only email-sending service which is reliable and scalable.. With SES, we can send transactional email, marketing messages, or any other type of high-quality content and we get carged only for what we use.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\"><strong>Demo<\/strong><\/h3>\n<p><\/br>In this demo, we will be integrating Amazon&#8217;s SES and postfix on a server i.e. all the mails from the server will be sent via the SES server.<\/p>\n<p>To know advantages of using AWS SES, visit this link:<br \/>\nhttp:\/\/docs.aws.amazon.com\/ses\/latest\/DeveloperGuide\/throughput-problems.html<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Use-Case<\/h3>\n<p><\/br>Sending a mail from a php application (php script) to the developer side or System Administrator, hence automating the alert in case of success\/failure.<\/p>\n<p>SMTP Support for the Amazon Simple Email Service (SES) is very useful. Now we can configure our server to send email through it regardless of what platform our site is built.<\/p>\n<p>There are 3 main things you need to do to configure your Postfix server to relay email through SES:<\/p>\n<ol>\n<li><strong>Verify a sender email address<\/strong><\/li>\n<li><strong>Create an IAM user for SMTP<\/strong><\/li>\n<li><strong>Configure your server to use SES<\/strong>.<\/li>\n<\/ol>\n<p><\/br><\/p>\n<h3 style=\"text-align: justify;color: #ff9900\"><strong>Prerequisites<\/strong><\/h3>\n<p><\/br>Before you perform the following procedures, verify the following:<\/p>\n<ul>\n<li>You have installed Postfix.<\/li>\n<\/ul>\n<p><\/br><\/p>\n<pre>sudo apt-get install postfix\r\n<\/pre>\n<ul>\n\t<\/br><\/p>\n<li>In case, you have Sendmail installed, it can be uninstalled by:<\/li>\n<\/ul>\n<p><\/br><\/p>\n<pre>sudo apt-get remove sendmail apt-get purge sendmail\r\n<\/pre>\n<ul>\n\t<\/br><\/p>\n<li>You are able to successfully send an email using Postfix without Amazon SES.<\/li>\n<\/ul>\n<ul>\n\t<\/br><\/p>\n<li>You have verified your &#8220;From&#8221; address and, if you do not yet have production access, you have also verified your &#8220;To&#8221; addresses.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>For steps to verify a sender &amp; reciever (as in our case, we don&#8217;t have Production access).<br \/>\n*In accounts with production access, we just need to verify sender&#8217;s email.<\/p>\n<p>1. In the SES section of the AWS Management Console, in Verified Senders click on \u201cEmail Addresses\u201d:<\/p>\n<p>2. Then click on the \u201cVerify a New Email Address\u201d button.<\/p>\n<p>3. Enter the Sender\u2019s Email Address and click \u201cVerify This Email Address\u201d.<\/p>\n<p>4. Then you\u2019ll see a confirmation message \u201cVerification Email Sent:<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/sesnn2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18391\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/sesnn2.png\" alt=\"sesnn2\" width=\"1343\" height=\"514\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>4. Go to that email account and click on the link Amazon will email to you to confirm the address.Click on the verification link to verify the email address.<\/p>\n<p>5. You should see the following screen, with both sender&#8217;s and receiver&#8217;s Email as verified.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/sesnn5.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18394\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/sesnn5.png\" alt=\"sesnn5\" width=\"1343\" height=\"306\" \/><\/a><\/p>\n<h3>To configure integration with the Amazon SES US East (Virginia) endpoint using STARTTLS<\/h3>\n<p><\/br>1. On your mail server, go to the \/etc\/postfix folder and open the main.cf file.<br \/>\n2. Add the following lines to the main.cf file, setting relayhost to the SMTP endpoint of the AWS region we are using.<\/p>\n<pre>relayhost = [email-smtp.us-east-1.amazonaws.com]:25\r\nsmtp_sasl_auth_enable = yes\r\nsmtp_sasl_security_options = noanonymous\r\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd\r\nsmtp_use_tls = yes\r\nsmtp_tls_security_level = encrypt\r\nsmtp_tls_note_starttls_offer = yes\r\n<\/pre>\n<p><\/br>Save and close the main.cf file.<\/p>\n<p>3. Now, open the master.cf file. This file resides in the \/etc\/postfix folder.<br \/>\n4. Comment out the following line of the master.cf file by putting a # in front of it:<\/p>\n<pre>-o smtp_fallback_relay=\r\n<\/pre>\n<p><\/br>Save and close the master.cf file.<\/p>\n<p>5. Edit the \/etc\/postfix\/sasl_passwd file. If the file does not exist, create it. Add the following lines to the file, replacing USERNAME and PASSWORD with your SMTP user name and password. Now this is where it gets confusing, you will want to create a SMTP User from the SES Console at:<\/p>\n<p><a title=\"Link to create a SES User\" href=\"https:\/\/console.aws.amazon.com\/ses\/home?#smtp-settings\" target=\"_blank\">https:\/\/console.aws.amazon.com\/ses\/home?#smtp-settings<\/a><\/p>\n<p>We will create a user here and be presented with the following Window (not from the IAM Console ):<\/p>\n<p>Go to the services tab (in your AWS Console) and select SES.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/sesssss.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18400\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/sesssss.png\" alt=\"sesssss\" width=\"1328\" height=\"256\" \/><\/a><\/p>\n<p>Select SMTP Settings from the left pane.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/SES.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18396\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/SES.png\" alt=\"SES\" width=\"1329\" height=\"429\" \/><\/a>Click on Create My SMTP Credentials.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/SES1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-18397\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/SES1.png\" alt=\"SES1\" width=\"1342\" height=\"768\" \/><\/a>Enter the user we wish to create (or leave it as default).We have given it a name ses-MyUser<br \/>\nClick on Create<\/p>\n<p>&nbsp;<\/p>\n<p>The next screen will look like this:<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/SES21.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/SES21.png\" alt=\"SES2\" width=\"1329\" height=\"259\" class=\"alignnone size-full\" \/><br \/>\nThe SMTP Username &amp; SMTP Password is what we require ( we may download it as well, for future use).<\/p>\n<p>Please NOTE: These credentials are an example and are now invalid, please do not use them.<\/p>\n<pre>email-smtp.us-east-1.amazonaws.com:25 USERNAME:PASSWORD\r\nses-smtp-prod-335357831.us-east-1.elb.amazonaws.com:25 USERNAME:PASSWORD\r\n<\/pre>\n<p><\/br>So it would be something like:<\/p>\n<pre>email-smtp.us-east-1.amazonaws.com:25 AXXXXXXXXXXXXXXXXXX:AXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \r\nses-smtp-prod-335357831.us-east-1.elb.amazonaws.com:25 AXXXXXXXXXXXXXXX:AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n<\/pre>\n<p><\/br><strong>Important<\/strong><br \/>\nUse your SMTP user name and password, not your AWS access key ID and secret access key. Your SMTP credentials and your AWS credentials are not the same.<\/p>\n<p>6. Save and close the sasl_passwd file.<br \/>\n7. At a command prompt, issue the following command to create a hashmap database file containing your SMTP credentials.<\/p>\n<pre>sudo postmap hash:\/etc\/postfix\/sasl_passwd\r\n<\/pre>\n<p><\/br>8. (Optional but recommended) Remove the \/etc\/postfix\/sasl_passwd file.<br \/>\n9. (Optional but recommended) The \/etc\/postfix\/sasl_passwd and \/etc\/postfix\/sasl_passwd.db files you created in the previous steps are not encrypted. Because these files contain your SMTP credentials, it is a good idea to use the following commands to change the owner to root and set permissions to restrict access to the files as much as possible. (Note that if you deleted \/etc\/postfix\/sasl_passwd in the previous step, you should omit it from the commands below.)<\/p>\n<pre>sudo chown root:root \/etc\/postfix\/sasl_passwd \/etc\/postfix\/sasl_passwd.db\r\nsudo chmod 0600 \/etc\/postfix\/sasl_passwd \/etc\/postfix\/sasl_passwd.db\r\n<\/pre>\n<p><\/br>10. Tell Postfix where to find the CA certificate (needed to verify the Amazon SES server certificate). We could also have used a self-signed certificate. We would use default certificates as follows:<\/p>\n<pre>sudo postconf -e 'smtp_tls_CAfile = \/etc\/ssl\/certs\/ca-certificates.crt'\r\n<\/pre>\n<p><\/br>11. When we have finished updating the configuration, we will stop and start Postfix by typing the following at the command line:<\/p>\n<pre>sudo postfix stop\r\nsudo postfix start\r\n<\/pre>\n<p><\/br>12. Create a simple php script to send a mail.<\/p>\n<p>&lt;?php<br \/>\n$to = &#8216;aankitgiri@gmail.com&#8217;;<br \/>\n$subject = &#8216;the subject&#8217;;<br \/>\n$message = &#8216;hello&#8217;;<br \/>\n$headers = &#8216;From: ankit.giri@intelligrape.com&#8217; . &#8220;\\r\\n&#8221; .<br \/>\n&#8216;Reply-To: ankit.giri@intelligrape.com&#8217; . &#8220;\\r\\n&#8221; .<br \/>\n&#8216;X-Mailer: PHP\/&#8217; . phpversion();<br \/>\nmail($to, $subject, $message, $headers);<br \/>\n?&gt;<\/p>\n<p>13. Check your inbox for the email. If the message was not delivered, check your Junk box, and then check your system&#8217;s mail log (typically \/var\/log\/maillog) for errors. For example, you will get an &#8220;Email address not verified&#8221; error if you have not verified the &#8220;From&#8221; address.<\/p>\n<p>So, now we have successfully configured our server to send mails through Amazon SES. Hope this helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Configuring server to relay email through Amazon SES will provide easy, real-time access to our sending statistics. It has built-in notifications for bounces, complaints, and deliveries which helps us fine-tune our email-sending strategy. Postfix is a free and open-source mail transfer agent (MTA) that routes and delivers electronic mail. Amazon Simple Email Service Amazon SES [&hellip;]<\/p>\n","protected":false},"author":166,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":120},"categories":[1174],"tags":[1700,1709,1708,1710,1699],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18387"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/166"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18387"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18387\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}