Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E-Mail Endpoint Change #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ gem 'delayed_job' # we use this to run jobs to index our data
gem 'mail' # Used for mail
gem 'mustache' # used for mail
gem 'premailer' # we use this to inline css in our emails
gem 'rest-client', :git => "https://github.com/archiloque/rest-client.git" # lets us send e-mail via Mailgun's HTTP API

# ActionView
gem 'sanitize' # used in app/controllers/posts_controller.rb (which is dead code) ! remove
Expand Down
10 changes: 8 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ GIT
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)

GIT
remote: https://github.com/archiloque/rest-client.git
revision: 4e5bd4893a3df02c82f56c432f746fd1d9c5830c
specs:
rest-client (1.6.7)
mime-types (>= 1.16)

GEM
remote: http://rubygems.org/
specs:
Expand Down Expand Up @@ -322,8 +329,6 @@ GEM
redis (>= 2.0.1)
resque (>= 1.8.0)
rufus-scheduler
rest-client (1.6.7)
mime-types (>= 1.16)
rmagick (2.13.1)
rollout (0.3.0)
rpm_contrib (2.1.4)
Expand Down Expand Up @@ -472,6 +477,7 @@ DEPENDENCIES
resque (~> 1.19.0)
resque-exceptional
resque-scheduler
rest-client!
rmagick
rollout
rpm_contrib
Expand Down
3 changes: 2 additions & 1 deletion config/initializers/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:domain => ENV['domain'],
:authentication => ENV['mail_authentication'],
:user_name => ENV['mail_username'],
:password => ENV['mail_password']
:password => ENV['mail_password'],
:api_key => ENV['mail_api_key']
}
end
49 changes: 49 additions & 0 deletions lib/mail_gun.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class MailGun

def self.deliver(method, options, to, from, reply_to, subject, body, content_type = "text/html", charset = "UTF-8", headers = {}, fake = false)
if (method == :file)
# STUB
return
elsif (method == :action_mailer)
# Send the e-mail with actionmailer
Mail.defaults do
delivery_method(method, options)
end
Mail.deliver(:to => to,
:from => from,
:reply_to => reply_to,
:subject => subject,
:content_type => content_type,
:body => body,
:charset => charset,
:headers => headers)
else
# Deliver with Mailgun
params = {}
params[:to] = to
params[:from] = from
params[:subject] = subject
params[:html] = body
if headers['X-Mailgun-Tag']
params[:tag] = headers['X-Mailgun-Tag']
headers.delete('X-Mailgun-Tag')
else
params[:tag] = ''
end
params["h:X-Reply-To"] = reply_to
headers.each do |k,v|
params["h:#{k}"] = v
end

endpoint = "https://api:#{options[:api_key]}@api.mailgun.net/v2/#{options[:domain]}/messages"
if fake
puts "Sending e-mail to endpoint #{endpoint} with parameters #{params.inspect}"
else
response = RestClient.post endpoint, {:params => params}
# Do something with the response...
Resque.redis.rpush("api_results", response.to_str)
end
end
end

end
31 changes: 13 additions & 18 deletions mail/mail_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
require 'premailer'
require 'sass'

Mail.defaults do
delivery_method($MailDeliveryMethod,
$MailDeliveryOptions)
end

class MailBase < Mustache
include MailUrls

Expand Down Expand Up @@ -110,19 +105,19 @@ def meets_limitation_requirement

def deliver
if deliver?
mail = Mail.deliver(:to => self.to,
:from => self.from,
:reply_to => self.reply_to,
:subject => self.subject,
:content_type => "text/html",
:body => self.render_html,
:charset => 'UTF-8',
:headers => {
"Precedence" => "list",
"Auto-Submitted" => "auto-generated",
"X-Campaign-Id" => community ? community.slug : "administrative",
"X-Mailgun-Tag" => self.tag
})
mail = MailGun.deliver($MailDeliveryMethod,
$MailDeliveryOptions,
:to => self.to,
:from => self.from,
:reply_to => self.reply_to,
:subject => self.subject,
:body => self.render_html,
:headers => {
"Auto-Submitted" => "auto-generated",
"Return-Path" => "<>",
"X-Campaign-Id" => community ? community.slug : "administrative",
"X-Mailgun-Tag" => self.tag
})
end
end

Expand Down