breaking into the unknown…

rails custom deliver method : sending email through API

Leave a comment

I have explained sending email using ActionMailer class of Rails Framework in this post . Go through it first if you not clear on how rail deliver the email. Now our requirement here is that, we do not want the rail to deliver the email for us, But want to get it deliver through ThirdParty email service, which say provide designed templates for the email or advertisement on the email or some other benefits. Basically, we want the email to be delivered through some API rather then Rails. So we need custom email delivery method

Here, you need to understand below two things how action mailer work.

=> ActionMailer create a email object which contain evrything you see in any email : subject, to, from, body, header etc

=> deliver method send the email object to destination using setting specified in the configuration file.

So, our approach will be to keep the first part of action mailer as it is,  and override the deliver method used in the second step.

NOTE : For complete detail on sending email through rails is explained in this post.Here I will mention only the steps which is needed to deliver email through custom method. Step 1 and Step 2 are new addition to the last post and step 3 will override the configuration used earlier. Other things will remain same.

Step 1 : Write Your class which interact with the API

Let us call this file postman.rb and place it in the lib folder of our rails application.

NOTE : define your own code within the deliver! (will throw exception if some error) or deliver (will not throw any exception) method. I have simply shown a sample code from one of my application. It will not work for you as I have used dummy password

module MyFinanace
  class Postman

    def deliver!(message)
      deliver_type = message.header["deliver_type"]
      if deliver_type == "api"
        url = "http://mansel/esb-sunpower/outbound/emailService/"
        username = "mensalTemplate"
        password = "gateway13"
        rest_resource =, username, password) prepare_payload(message), :content_type => "application/xml"
        mail.perform_deliveries = false

     def prepare_payload message
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <cc>#{';') unless}</cc>
            <bcc>#{message.bcc.join(';') unless message.bcc.nil?}</bcc>
            <replyTo>#{message.reply_to.first.to_s unless message.reply_to.nil?}</replyTo>
            <subject>#{message.subject.to_s unless message.subject.nil?}</subject>


So you can see that, We have redefined the deliver! method and making API call within it to mansel service. We pass it all the required detail like subject, to, from, body etc in the payload and left up to mansel to deliver the message.

STEP 2: Add your class defined above to the delivery method of rails ActionMailer class

Rails by default support smtp delivery. Since we have added our own class to deliver the email we need to tell ActionMailer about it. We can achieve it with add_delivery_method of ActionMailer . We need to specify it the time our application boot up, so be will create a file say my_mailer.rb in initializer folder of our application and add below lines to it.

require 'postman'
ActionMailer::Base.add_delivery_method :my_finanace_smtp, MyFinanace::Postman

So we have named our custom delivery method as my_finanace_smtp and pointed it to Postman module of MyFinanace class . Now, whenever we start our application, we will have our custom delivery method available to ActionMailer.

STEP 3: configure your environment file to use your custom deliver method

Since now we are using custom deliver method , which will be handled by API outside our rails application, We will replace the configuration used in development.rb file in step 1 of this post with below two line.

  config.action_mailer.default_url_options = {:host => 'localhost:3000'}
  config.action_mailer.delivery_method = :my_finanace_smtp

That’s all, now everything will remain same as in the last post, but your email will be delivered through your custom email deliver method.


Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s