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

Written by Mailgun Team

Categories: Email DIY

2 minute read time

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 => "",
     :port => 587,
     :domain => "",
     :user_name => "",
     :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 2822 and 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]@[]>”

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(}"

   # ... the rest of your mailer class

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

Happy emailing!


Modified on: March 13, 2019

Stay up-to-date with our blog & new email resources

We'll let you know when we add new email resources and blog posts. We promise not to spam you.