From 1b8ccc4cd4d2121a87e0adff9f8278df0a654785 Mon Sep 17 00:00:00 2001 From: Paul Bunkham Date: Thu, 3 Sep 2015 21:26:17 +0100 Subject: [PATCH 1/3] Added support for sending mail through Mandrill --- Gemfile | 2 ++ Gemfile.lock | 24 ++++++++++++++++++++++++ lib/gaps/email.rb | 25 +++++++++++++++++++++++-- site.yaml.sample | 14 ++++++++++---- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 80b0658..c27da07 100644 --- a/Gemfile +++ b/Gemfile @@ -24,9 +24,11 @@ gem 'configatron' gem 'pry' gem 'rest-client' gem 'mail' +gem 'mandrill-api' gem 'gmail-britta', :git => 'git://github.com/bkrausz/gmail-britta.git', :ref => 'ced6355443fe2b99f6414b2da096c70a38b23f98' group :development, :test do gem 'mocha' + gem 'rerun' end diff --git a/Gemfile.lock b/Gemfile.lock index 1f55b54..6d9bc2e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,6 +27,8 @@ GEM bson_ext (1.11.1) bson (~> 1.11.1) builder (3.2.2) + celluloid (0.16.0) + timers (~> 4.0.0) chalk-config (0.2.1) configatron (~> 4.4) chalk-log (0.1.2) @@ -37,9 +39,11 @@ GEM configatron (4.4.1) einhorn (0.6.3) erubis (2.7.0) + excon (0.45.4) extlib (0.9.16) faraday (0.9.0) multipart-post (>= 1.2, < 3) + ffi (1.9.10) google-api-client (0.7.1) addressable (>= 2.3.2) autoparse (>= 0.3.3) @@ -52,11 +56,16 @@ GEM signet (>= 0.5.0) uuidtools (>= 2.1.0) haml (3.1.8) + hitimes (1.2.2) i18n (0.6.11) json (1.8.1) jwt (1.2.0) launchy (2.4.3) addressable (~> 2.3) + listen (2.10.1) + celluloid (~> 0.16.0) + rb-fsevent (>= 0.9.3) + rb-inotify (>= 0.9) little-plugger (1.1.3) logging (1.8.2) little-plugger (>= 1.1.3) @@ -64,6 +73,9 @@ GEM lspace (0.13) mail (2.6.3) mime-types (>= 1.16, < 3) + mandrill-api (1.0.53) + excon (>= 0.16.0, < 1.0) + json (>= 1.7.7, < 2.0) metaclass (0.0.4) method_source (0.8.2) mime-types (2.4.3) @@ -96,6 +108,11 @@ GEM rack_csrf (2.5.0) rack (>= 1.1.0) rake (10.4.0) + rb-fsevent (0.9.6) + rb-inotify (0.9.5) + ffi (>= 0.5.0) + rerun (0.10.0) + listen (~> 2.7, >= 2.7.3) rest-client (1.7.2) mime-types (>= 1.16, < 3.0) netrc (~> 0.7) @@ -113,6 +130,8 @@ GEM thread (0.1.4) thread_safe (0.3.4) tilt (1.4.1) + timers (4.0.4) + hitimes tzinfo (1.2.2) thread_safe (~> 0.1) uuidtools (2.1.5) @@ -130,6 +149,7 @@ DEPENDENCIES gmail-britta! google-api-client mail + mandrill-api mocha mongo_mapper pry @@ -137,6 +157,10 @@ DEPENDENCIES rack-flash3 rack_csrf rake + rerun rest-client sinatra thread + +BUNDLED WITH + 1.10.6 diff --git a/lib/gaps/email.rb b/lib/gaps/email.rb index 3d9a3e6..353632b 100644 --- a/lib/gaps/email.rb +++ b/lib/gaps/email.rb @@ -1,5 +1,6 @@ require 'restclient' require 'mail' +require 'mandrill' module Gaps module Email @@ -9,8 +10,28 @@ def self.send_email(opts) mail = Mail.new(opts) if configatron.notify.send_email - mail.delivery_method :sendmail - mail.deliver! + if configatron.notify.provider == "mandrill" + begin + mandrill = Mandrill::API.new configatron.notify.api_key + message = { + "from_email"=>opts[:from], + "text"=>opts[:body], + "to"=> [{"email"=>opts[:to], + "type"=>"to"}], + "subject"=>opts[:subject], + } + async = false + result = mandrill.messages.send message + + rescue Mandrill::Error => e + # Mandrill errors are thrown as exceptions + log.error "A mandrill error occurred: #{e.class} - #{e.message}" + raise + end + else + mail.delivery_method :sendmail + mail.deliver! + end else log.info("Would have sent", email: mail.to_s) end diff --git a/site.yaml.sample b/site.yaml.sample index 7694ca5..6c27cdc 100644 --- a/site.yaml.sample +++ b/site.yaml.sample @@ -4,7 +4,9 @@ db: # URL to connect to your MongoDB instance (where all state is # persisted). Note that Gaps stores some interesting state, such as # people's filter configuration and group categorization. - mongodb_url: mongodb://localhost:27017 + # Set the host to gaps-mongo by default as that's how the + # docker-runner script is configured + mongodb_url: mongodb://gaps-mongo:27017 # Your favicon favicon: @@ -21,10 +23,14 @@ info: notify: # Whether to send an email notifying people when a new list is # created. If disabled, will instead just print the email to the - # console. (Note you must have a functioning sendmail setup for this - # to work; a TODO is to integrate with an email provider like - # Mailgun.) + # console. + # Can use sendmail or Mandrill to send emails send_email: false + # Default provider + provider: sendmail + # Mandrill - need to include and API key + #provider: mandrill + #api_key: xxxxxxxx # What email address to notify. to: all-bots@example.com # What email address to send from. In Stripe's email transparency From 2f8ca3b49260118b12354dea3b5a392e525a4b11 Mon Sep 17 00:00:00 2001 From: Paul Bunkham Date: Fri, 4 Sep 2015 10:58:15 +0100 Subject: [PATCH 2/3] Test script for sending an email through Mandrill (bit hacky) --- bin/test_mandrill.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100755 bin/test_mandrill.rb diff --git a/bin/test_mandrill.rb b/bin/test_mandrill.rb new file mode 100755 index 0000000..a201aec --- /dev/null +++ b/bin/test_mandrill.rb @@ -0,0 +1,18 @@ +#!/usr/bin/env ruby +require File.expand_path('../lib/gaps', File.dirname(__FILE__)) + +Gaps.init + +Gaps::Email.send_email( + :to => configatron.notify.to, + :from => configatron.notify.from, + :subject => "[gaps] Testing email", + :body => < Date: Fri, 4 Sep 2015 11:07:12 +0100 Subject: [PATCH 3/3] Backing out change in site.yaml.sample --- site.yaml.sample | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/site.yaml.sample b/site.yaml.sample index 6c27cdc..d6d2abf 100644 --- a/site.yaml.sample +++ b/site.yaml.sample @@ -4,9 +4,7 @@ db: # URL to connect to your MongoDB instance (where all state is # persisted). Note that Gaps stores some interesting state, such as # people's filter configuration and group categorization. - # Set the host to gaps-mongo by default as that's how the - # docker-runner script is configured - mongodb_url: mongodb://gaps-mongo:27017 + mongodb_url: mongodb://localhost:27017 # Your favicon favicon: