Skip to content

Instantly share code, notes, and snippets.

@rdetert
Created August 22, 2014 04:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdetert/59187741560302754875 to your computer and use it in GitHub Desktop.
Save rdetert/59187741560302754875 to your computer and use it in GitHub Desktop.
AOL and Yahoo! now use DMARC to force email services like SendGrid and MailChimp to reject emails if the FROM field gets 'spoofed' for their domain. To get around this, we will check for the `p=reject` key/value combo in the TXT record on _dmarc.domain.com. Here is a quick and dirty way to check if the DMARC field is set using ActionMailer on Ru…
class ActionMailer::Base
def dmarc(email)
domain = email.split('@')[1]
dmarc_domain = "_dmarc.#{domain}"
Resolv::DNS.open do |dns|
records = dns.getresources(dmarc_domain, Resolv::DNS::Resource::IN::TXT)
records.empty? ? nil : records.map(&:data).join(" ")
end
end
def dmarc?(email, key, value)
key = key.to_s
value = value.to_s
record = dmarc email
return false if record.blank?
record_items = record.split(';').map(&:strip)
hash = {}
record_items.each do |item|
k,v = item.split('=')
hash[k] = v
end
hash[key] == value
end
end
class SupportEmailsController < ApplicationController
def create
@support_email = SupportEmail.new post_params
if @support_email.save
SupportMailer.regular(@support_email).deliver
flash[:notice] = "Thanks for contacting us! We'll get back to you as soon as possible."
else
flash[:alert] = "There was a problem sending. Please email us directly"
end
redirect_to support_path
end
protected
def post_params
safe_params = [
:from, :body, :subject
]
params.require(:support_email).permit(*safe_params)
end
end
class SupportMailer < ActionMailer::Base
default :to => "support@mydomain.com"
default :from => "support@mydomain.com"
def regular(support_email)
@support_email = support_email
options = {
subject: support_email.subject
}
if dmarc?(support_email.from, :p, :reject)
options[:reply_to] = support_email.from
else
options[:from] = support_email.from
end
@mail = mail( options ) do |format|
format.text
format.html { render :layout => false }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment