Free Consultation

How to use Amazon SES for sending an email without domain verification

How to use Amazon Simple Email Service as an easy, cost-effective way to send or receive email using your own email addresses in Ruby on Rails

Posted by Ameena on 24 May 2020

Sending an email to the customers is an important feature in almost every application of 2020 and also to maintain that the email does not land in the spam folder.

There are multiple services like Sendgrid, Mailgun, SES available for sending emails. In order to make sure your email doesn't end in spam, you either have to verify the domain or email address.

Domain verification
You have to verify your domain which will be sending emails. For example, if emails will be sent from ameena@inkoop.io, then you have to verify the domain inkoop.io. For this you need access to edit DNS of the domain.

Email address verification
But what if you don't have access to the domain's DNS and still would want to send email to the customers then Email verification comes in handy. And you can definitely opt Amazon Simple Email Service as it is easy and cost-efficient.

Amazon Simple Email Service (Amazon SES) is a cloud-based email sending service designed to help digital marketers and application developers send marketing, notification, and transactional emails.

To send an email to the customers using AWS SES, we have to follow these two steps

  1. Verify an email address using custom verification Email Templates
  2. Send Email

1) Verify an email address using custom verification Email Templates

To verify any email address we need to use the below methods

  1. createcustomverificationemailtemplate
  2. sendcustomverification_email

Before we start using any of the methods of Amazon SES, create a client object of AWS SES. Sample Code is written using Ruby.

#aws_ses.rb

class AwsSes

  private
  def ses_client
    @ses ||= Aws::SES::Client.new(
      region: region_name,
      credentials: credentials,
      # ...
    )
  end
end

Now that you have an object of SES you can create a template that can be used to send a request to authorize the email address.

Also, you need to have one email verified in AWS SES to create a custom verification email template. This can be done by logging in into your AWS account.

Consider the email address as no-reply@inkoop.io which is verified.

#aws_ses.rb

class AwsSes

  def create_custom_verification_email_template
    begin
      ses_client.create_custom_verification_email_template({
        template_name: "SES_TEMPLATE_NAME",
        from_email_address: "no-reply@inkoop.io",
        template_subject: "Email Address Verification Request",
        template_content: '<html><body><div>Dear partner,</div><br><div>
        ...
        </div></body></html>',
        success_redirection_url: "https://inkoop.io/ses/confirmed",
        failure_redirection_url: "https://inkoop.io/ses/failed",
      })
    rescue StandardError => e
      Airbrake.notify(e)
    end
  end

  private
  def ses_client
    @ses ||= Aws::SES::Client.new(
      region: region_name,
      credentials: credentials,
      # ...
    )
  end
end

fromemailaddress should be a verified email address of AWS SES.
successredirectionurl is the URL that is redirected to after the user clicks on the URL to authorize.

For more information about the parameters, you can see here

Now that the custom email template is created. We can send a verification email to the user. When the user clicks on the success redirection URL, the email address is verified.

#aws_ses.rb

class AwsSes
...
  def send_custom_verification_email
    begin
      ses_client.send_custom_verification_email({
        email_address: "ameena@inkoop.io",
        template_name: "SES_TEMPLATE_NAME"
      })
      true
    rescue StandardError => e
      Airbrake.notify(e)
    end
  end
end
...

Remember to keep the template_name same as the one you created using createcustomverificationemailtemplate and email_adress here should be the one whose email has to be verified to send emails from your application further.

In order to check if the email is verified or not you can use this method:

#aws_ses.rb

class AwsSes
...
  def get_verified_emails
    result = ses_client.list_verified_email_addresses
    result.verified_email_addresses
  end
...
end

2) Send an email

You can use this method finally to send emails to the customers. send_email() composes an email message and immediately queues it for sending.

#aws_ses.rb

class AwsSes
...
  def send_email
    begin
      from = "ameena@inkoop.io"
      ses_client.send_email({
        source: from,
        destination: {
          to_addresses: [customer.email]
        },
        message: {
          subject: {
            data: "subject"
          },
          body: {
            html: {
              data: "body"
            }
          },
        },
        reply_to_addresses: from,
      })
    rescue StandardError => e
      Airbrake.notify(e)
    end
  end
end
...

body of the email can be HTML or plain text. For more information about the parameters you can read here.

I hope this blog helps you.

Keep Coding !!!

Contact us to work on your website.

- Ameena


We support the Open Source community.



VueJS Logo
Bulma Logo

Have a Project in mind?