From 9ba775b2baa2ad26e3aadd3606d881575e457981 Mon Sep 17 00:00:00 2001 From: Jason Berlinsky Date: Fri, 21 Oct 2011 13:54:04 -0400 Subject: [PATCH 1/3] Starts moving e-mails to Mailgun's HTTP POST interface --- config/initializers/mail.rb | 3 ++- lib/mail_gun.rb | 28 ++++++++++++++++++++++++++++ mail/mail_base.rb | 31 +++++++++++++------------------ 3 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 lib/mail_gun.rb diff --git a/config/initializers/mail.rb b/config/initializers/mail.rb index c32807629..62d7944f8 100644 --- a/config/initializers/mail.rb +++ b/config/initializers/mail.rb @@ -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 diff --git a/lib/mail_gun.rb b/lib/mail_gun.rb new file mode 100644 index 000000000..be6933d6e --- /dev/null +++ b/lib/mail_gun.rb @@ -0,0 +1,28 @@ +class MailGun + + def self.deliver(method, options, to, from, reply_to, subject, content_type = "text/html", body, charset = "UTF-8", headers = {}) + if (method == :file) + # Save the e-mail to a file + # STUB + else if (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 + response = RestClient.post "https://api:#{options[:api_key]}@api.mailgun.net/v2/#{options[:domain]}/messages", :from => from, :to => to, :subject => subject, :html => body, :tag => ((headers['X-Milgun-Tag'].present?) ? headers['X-Mailgun-Tag']: ""), "h:X-Reply-To" => reply_to, "h:X-Precedence", "h:Auto-Submitted" => ((headers["Auto-Submitted"].present?) ? headers["Auto-Submitted"] : ""), "h:Return-Path" => ((headers["Return-Path"].present?) ? headers["Return-Path"] : ""), "h:X-Campaign-ID" => ((headers["X-Campaign-Id"].present?) ? headers["X-Campaign-Id"] : "") + # Do something with the response... + Resque.redis.rpush("api_results", response.to_str) + end + end + +end diff --git a/mail/mail_base.rb b/mail/mail_base.rb index b8be05108..80039be28 100644 --- a/mail/mail_base.rb +++ b/mail/mail_base.rb @@ -2,11 +2,6 @@ require 'premailer' require 'sass' -Mail.defaults do - delivery_method($MailDeliveryMethod, - $MailDeliveryOptions) -end - class MailBase < Mustache include MailUrls @@ -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 From 5e1f115e08f4297a199987e3a8947d92847b4700 Mon Sep 17 00:00:00 2001 From: Jason Berlinsky Date: Fri, 21 Oct 2011 14:10:33 -0400 Subject: [PATCH 2/3] Adds RestClient --- Gemfile | 1 + Gemfile.lock | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index c7bf04956..d105aa8e6 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 412eaa685..8ffba33f2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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: @@ -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) @@ -472,6 +477,7 @@ DEPENDENCIES resque (~> 1.19.0) resque-exceptional resque-scheduler + rest-client! rmagick rollout rpm_contrib From eb95a297d2894c8e4eb34639336875b55d63e4cd Mon Sep 17 00:00:00 2001 From: Jason Berlinsky Date: Fri, 21 Oct 2011 14:35:19 -0400 Subject: [PATCH 3/3] Fix syntax --- lib/mail_gun.rb | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/mail_gun.rb b/lib/mail_gun.rb index be6933d6e..be995e6da 100644 --- a/lib/mail_gun.rb +++ b/lib/mail_gun.rb @@ -1,10 +1,10 @@ class MailGun - def self.deliver(method, options, to, from, reply_to, subject, content_type = "text/html", body, charset = "UTF-8", headers = {}) + def self.deliver(method, options, to, from, reply_to, subject, body, content_type = "text/html", charset = "UTF-8", headers = {}, fake = false) if (method == :file) - # Save the e-mail to a file # STUB - else if (method == :action_mailer) + return + elsif (method == :action_mailer) # Send the e-mail with actionmailer Mail.defaults do delivery_method(method, options) @@ -19,9 +19,30 @@ def self.deliver(method, options, to, from, reply_to, subject, content_type = "t :headers => headers) else # Deliver with Mailgun - response = RestClient.post "https://api:#{options[:api_key]}@api.mailgun.net/v2/#{options[:domain]}/messages", :from => from, :to => to, :subject => subject, :html => body, :tag => ((headers['X-Milgun-Tag'].present?) ? headers['X-Mailgun-Tag']: ""), "h:X-Reply-To" => reply_to, "h:X-Precedence", "h:Auto-Submitted" => ((headers["Auto-Submitted"].present?) ? headers["Auto-Submitted"] : ""), "h:Return-Path" => ((headers["Return-Path"].present?) ? headers["Return-Path"] : ""), "h:X-Campaign-ID" => ((headers["X-Campaign-Id"].present?) ? headers["X-Campaign-Id"] : "") - # Do something with the response... - Resque.redis.rpush("api_results", response.to_str) + 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