Skip to content

Instantly share code, notes, and snippets.

@bonkydog
Created August 24, 2011 06:23
Show Gist options
  • Save bonkydog/1167417 to your computer and use it in GitHub Desktop.
Save bonkydog/1167417 to your computer and use it in GitHub Desktop.
Messagebus ActionMailer adapter
# in your environment file (for instance, production.rb)
MyAwesomeApp::Application.configure do
# ...lots of configuration...
config.action_mailer.delivery_method = Messagebus::Mailer.new(ENV['MESSAGEBUS_API_KEY'])
end
# lib/messagebus/mailer.rb
module Messagebus
class Mailer
def initialize(api_key)
@client = MessagebusRubyApi::Client.new(api_key)
end
attr_accessor :settings
def new(*settings)
self
end
def deliver!(message)
deliver(message)
end
private
def deliver(message)
@client.common_info = {:fromEmail => message.from.first}
message.to.each do |addressee|
m = {:toEmail => addressee, :subject => message.subject}
if message.multipart?
m[:plaintextBody] = message.text_part.body.to_s if message.text_part
m[:htmlBody] = message.html_part.body.to_s if message.html_part
else
m[:plaintextBody] = message.body.to_s
end
@client.add_message(m)
end
status = @client.flush
if status[:failureCount] && status[:failureCount] > 0
raise "Messagebus failure. failureCount=#{failureCount}, message=#{message.inspect}"
end
end
end
end
@maxwell
Copy link

maxwell commented Oct 4, 2011

Are we supposed to subclass our Mailer classes with this, or can we point some sort of ActionMailer setting at this?

@bonkydog
Copy link
Author

bonkydog commented Oct 5, 2011

Ooops! I suppose that's not obvious.

Just set config.action_mailer.deliver_method in your environment file like this:

MyAwesomeApp::Application.configure do

... lotsa configuration settings...

config.action_mailer.delivery_method = Messagebus::Mailer.new(ENV['MESSAGEBUS_API_KEY'])
end

@maxwell
Copy link

maxwell commented Oct 5, 2011

Thanks! Any idea if things like Devise mailer will pick up on this setting? Also, will the normal test deliveries Just Work™? :)

@bonkydog
Copy link
Author

bonkydog commented Oct 5, 2011

Yeah, Devise just uses ActionMailer like everything else.

It sends password reset emails just fine using this setup on gallerystar.com.

For testing we use the email_spec gem.

@maxwell
Copy link

maxwell commented Oct 5, 2011

Thanks for the info! What ref of the message_bus_api are you using? I am getting NoMethodError: undefined method `common_info=' for #MessagebusRubyApi::Client:0x106bde690 with the latest master.

@maxwell
Copy link

maxwell commented Oct 5, 2011

ok it looks like it is now send_common_info

@bonkydog
Copy link
Author

bonkydog commented Oct 5, 2011

messagebus_ruby_api (0.4.0)

@maxwell
Copy link

maxwell commented Oct 5, 2011

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment