Email

Tips & tricks: Avoiding Gmail spam filtering when using Ruby on Rails Action Mailer

It has become clear from our support logs that a lot of developers use Action Mailer to send emails. If you are one of these developers you should know that there is one issue that we have seen repeatedly which will cause your emails to get filtered as spam by Gmail: malformed message-ID. This also may occur with other libraries but we see this issue mostly with Action Mailer.

PUBLISHED ON

PUBLISHED ON

It has become clear from our support logs that a lot of developers use Action Mailer to send emails. If you are one of these developers you should know that there is one issue that we have seen repeatedly which will cause your emails to get filtered as spam by Gmail: malformed message-ID. This also may occur with other libraries but we see this issue mostly with Action Mailer.

We created our REST API so that you don’t have to worry about technicalities like these (we set a valid message-ID by default) but we understand that you may want to use the tools you are already familiar with.

If you want to use Action Mailer with Mailgun, it’s pretty easy to get started:

config.action_mailer.delivery_method = :smtp config.action_mailer.default_charset = "utf-8" config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true config.action_mailer.smtp_settings = { :authentication => :plain, :address => "smtp.mailgun.org", :port => 587, :domain => "my-mailgun-domain.com", :user_name => "postmaster@my-mailgun-domain.com", :password => "my-password" }

The issue is that if you do not specify the message-ID, Action Mailer populates this header with a default value which may not be a valid message-ID. In addition, if you are connecting to an SMTP relay, that message-ID probably does not reflect the domain that is actually sending the email. It may say something like “<[uid]@localhost.localdomain>”.

Gmail is very strict about making sure that the message-ID in the MIME headers follows RFC 2822and that the domain in the message-ID is the same as the sending domain.

According to the RFC the message-ID should have the following format: “<[uid]@[sendingdomain.com]>”

There is a thread on Stack Overflow about how to set the message-ID in Rails, see example excerpt below:

require 'digest/sha2' class UserMailer < ActionMailer::Base default "Message-ID"=>"#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}@yourdomain.com" # ... the rest of your mailer class end

So, make sure to test that the message-ID is getting set correctly or Gmail will junk your emails!

Happy emailing!

Mailgunners

Sign Up

It's easy to get started. And it's free.

See what you can accomplish with the world’s best email delivery platform.

Related readings

Key takeaways from Mailgun’s State of email deliverability 2023

Why do some emails reach the inbox, some land in spam, and others are blocked from delivery? What can you do to improve your chances of getting messages into the inbox...

Read more

What is inbox placement and how is it calculated?

Email deliverability sometimes feels like a mirage. Your messages go on a long journey from your mail server to your subscribers’ inboxes. But it’s the last part of that journey that...

Read more

How to keep your spam complaint rate low

It feels like a slap in the face. Or being dumped with the “it’s not you; it’s me” line. When an email subscriber marks one of your emails as spam, it feels like a betrayal. We were doing...

Read more

Popular posts

Email inbox.

Build Laravel 10 email authentication with Mailgun and Digital Ocean

When it was first released, Laravel version 5.7 added a new capability to verify user’s emails. If you’ve ever run php artisan make:auth within a Laravel app you’ll know the...

Read more

Mailgun statistics.

Sending email using the Mailgun PHP API

It’s been a while since the Mailgun PHP SDK came around, and we’ve seen lots of changes: new functionalities, new integrations built on top, new API endpoints…yet the core of PHP...

Read more

Statistics on deliverability.

Here’s everything you need to know about DNS blocklists

The word “blocklist” can almost seem like something out of a movie – a little dramatic, silly, and a little unreal. Unfortunately, in the real world, blocklists are definitely something you...

Read more

See what you can accomplish with the world's best email delivery platform. It's easy to get started.Let's get sending
CTA icon