Four different scripts to send email 📬using AWS-SES(Simple Email Service) — Part 1😎

Peal Ami🤓
codeburst
Published in
5 min readJun 16, 2017

--

This is Part — 1 which includes,

  • Why AWS-SES
  • Price Comparison
  • How to get SES Credential
  • SMTP Settings
  • Configure SES in Ruby on Rails and Python

Why Amazon SES? Why people are moving from other mail clients to Amazon-SES?

  • Most important thing is cost-effective email service.
  • More reliable and scalable infrastructure

With Amazon SES, you have no required minimum commitments — you pay as you go and only pay for what you use.

And this is great option if you need to send a lot of email within a budget.

Here is a list of price comparison of other mail clients,

Price Comparison

NOTE: If you have an account in Amazon web service just login. If not, signup as a new user by following my previous blog.

  1. Login to AWS management console.
AWS Login

2. It will take you to the AWS Dashboard, look for SES.

AWS Dashboard

3. Click on “SES” to go to the Amazon SES Dashboard. And Click on “Email Addresses”

SES Home

4. Verify two email addresses (You must have two email addresses to complete the email sending flow in sandbox mode). I have already added two emails and verified them.

SES Email Adresses

Click on Verify a New Email Address button to add your sending and receiving email addresses.

Verify a New Email Address

5. SES will send you a confirmation email, click the confirmation link in your mail to verify your email address.

6. Create your SMTP credentials to use in your app.

SES SMTP Credentials

7. After Click on the “Create My SMTP Credentials” button shown in the picture above.

Create User for SMTP

You can change this username if you want, but this default one will do the trick for now.

Okay now we all set to configure in different languages. Now we have AWS-SES credentials.

1.Lets see how to configure SES in ruby on rails application.

— In your controller add the below method,

class WelcomeController < ApplicationController
def send_email
@email = MyMailer.send_email_by(params[“name”],params[“email”])
render :json => @email
end
end

— Let’s generate a mailer by using this command rails generate mailer my_mailer

It Will generate two .rb files, application_mailer.rb and my_mailer.rb

In app/mailers/my_mailer.rb put the below code, and into set your sender email address.

class MyMailer < ApplicationMailer
def send_email_by(name,email)
mail(:to=>"sender_mail_address", :subject=>"Amazon SES Email")
end
end

After adding the send_email action to our mailer file, change the default from email address in app/mailers/application_mailer.rb file to the address you wish to use. In this tutorial, I’ll call it example@mailinator.com.

class ApplicationMailer < ActionMailer::Base
default from: 'example@mailinator.com'
layout 'mailer'
end

Open up config/environments/development.rb in order to change the SMTP configuration and test the emails out in the local development environment.

To deploying this application to a production environment put the SMTP configuration in config/environments/prodution.rb, thats it 😜.

We’re just going to go through the Amazon SES setup, code and configuration to get our app ready to send 62,000 free email per month from an Amazon EC2 instance.

Head over to the config/environments/development.rb and make these changes to get our app ready to send emails.

First of all set action_mailer.perform_deliveries to true as it is set to false by default in the development environment,

config.action_mailer.perform_deliveries = true

Next, look for this line,

config.action_mailer.raise_delivery_errors = false

and set it’s value to true. This will help debug delivery errors if our email fails to deliver for some reason.

Now paste the SMTP settings info the below configuration block,

config.action_mailer.smtp_settings = {
:address => "...",
:port => 587,
:user_name => ENV["SES_SMTP_USERNAME"], #Your SMTP user
:password => ENV["SES_SMTP_PASSWORD"], #Your SMTP password
:authentication => :login,
:enable_starttls_auto => true
}

Copy the Server Name from SMTP Credential and put it in the action_mailer.smtp_settings block in config/environments/development.rb. Replace this line

:address => "<ses server name>" 

with the Server Name you copied from the SMTP Settings page.

Now the action_mailer.smtp_settings block should look similar to this,

config.action_mailer.smtp_settings = {
:address => "email-smtp.us-east-1.amazonaws.com",
:port => 587,
:user_name => ENV["SES_SMTP_USERNAME"], #Your SMTP user
:password => ENV["SES_SMTP_PASSWORD"], #Your SMTP password
:authentication => :login,
:enable_starttls_auto => true
}

Click on the “Create My SMTP Credentials” button shown in the picture above.

SMTP User Credentials

Copy these credentials and paste into your config/environments/development.rb or, better yet, export them in your environment,

ENV["SES_SMTP_USERNAME"] = "*****" # Replace with your SMTP username
ENV["SES_SMTP_PASSWORD"] = "*****" # Replace with your SMTP password

We are all set to send emails now.

If you want to design in view here is a reference link to send email in ruby on rails using AWS-SES.

2.Now we can see how to configure SES in python application.

Here below I have a script that I used to send email via SES, Create a function and just copy the below script and paste it into your function.

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# AWS Config
EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com'
EMAIL_HOST_USER = "*****" # Replace with your SMTP username
EMAIL_HOST_PASSWORD = "*****" # Replace with your SMTP password
EMAIL_PORT = 587

msg = MIMEMultipart('alternative')
msg['Subject'] = "Amazon SES Email"
msg['From'] = "example@mailinator.com"
msg['To'] = "sender_mail_address"

html = open('index.html').read()

mime_text = MIMEText(html, 'html')
msg.attach(mime_text)

s = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
s.starttls()
s.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
s.sendmail(me, you, msg.as_string())
s.quit()

Thats it, Simple right 😃

Hope this part-1 will help you, Will see you in the next part with two other different scripts😇 .

Happiee Email Sending…. 📮

--

--

Backend Developer #RoR❤️ #node💚 Loves Animated movies, Loves to ride #cyclelover🚴‍♀️ #royalenfieldlover🏍