1

I currently have Rails applications that use SES to send emails. Unfortunately no matter how much code I put in my application I still get emails with invalid email addresses.

I want to use AWS to verify if I have a valid email address, meaning that the syntax is correct and does other verification like checking if the mailbox exists.

I installed the aws-sdk-rails gem in my application. I added my access_key_id and secret_access_key to config/credentials.yml.enc.

I added the following code from this awsdocs/aws-doc-sdk-examples GitHub repository to my contact form and made minor changes.

require 'aws-sdk-ses'

# Replace recipient@example.com with a "To" address.
recipient = params[:email]
error = " "

# Create a new SES resource in the us-west-2 region.
# Replace us-west-2 with the AWS Region you're using for Amazon SES.
ses = Aws::SES::Client.new(region: 'us-west-2')

# Try to verify email address.
begin
  ses.verify_email_identity({
    email_address: recipient
  })

  puts 'Email sent to ' + recipient

# If something goes wrong, display an error message.
rescue Aws::SES::Errors::ServiceError => error
  puts "Email not sent. Error message: #{error}"
end

I entered an email address that AWS SES said that the mailbox didn't exist this morning. However when I ran this code it didn't produce an error as I thought it might. When I checked the Rails debug log error was blank. The region in my code is the one that I use to successfully send transactional emails from all my websites.

I couldn't find any documentation about that code that says how much verification it does for the email address.

Can I use this gem to find if email addresses exist or have other problems like SES checks when an email has a To: email address?

  • 1
    When you say that the email doesn't exist have you tried to send to that address and then get some sort of bounce back? – stdunbar Sep 13 '22 at 19:01
  • Yes. I’ve used AWS SES for several years before installing the gem. This morning I received an email from AWS that said an email couldn’t be sent because the mailbox didn’t exist. I tried using that email address with the example code provided by AWS and it didn’t identify that the email doesn’t exist. The documentation is not clear if that code should do that or not. – Pamela Cook - LightBe Corp Sep 14 '22 at 00:03
  • Validating an email address is a pain as you're likely aware. SES can accept the email and not know until a bit later that the address is not valid. Do you have [SES events](https://docs.aws.amazon.com/ses/latest/dg/monitor-using-event-publishing.html) enabled? You need to send with a "configuration set" but other than that you can get more information back about, for example, a [bounce](https://docs.aws.amazon.com/ses/latest/dg/event-publishing-retrieving-sns-contents.html#event-publishing-retrieving-sns-contents-bounce-object). I've used that to remove addresses from my database. – stdunbar Sep 14 '22 at 14:30
  • @stdunbar I just got another spam email at another email address that doesn't exist. I use database tables to check emails and domains which I don't like but at least it slowed down the spam. I tried SES events long ago but couldn't get it working but I will definitely try again. I want to check more than just bounces. When I get it working I will post what I did. – Pamela Cook - LightBe Corp Sep 16 '22 at 11:42

0 Answers0