Created
August 22, 2014 04:15
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SupportMailer < ActionMailer::Base | |
default :to => "[email protected]" | |
default :from => "[email protected]" | |
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