Skip to content

Latest commit

 

History

History
58 lines (44 loc) · 1.4 KB

usage_of_mailgun_api_in_ruby_on_rails.md

File metadata and controls

58 lines (44 loc) · 1.4 KB

How to use Mailgun API with MailPlugger in Ruby on Rails

Please note that these examples were not tested, but I believe it should work.

Let's use mailer method which was defined here.

Add mailgun-ruby gem to the Gemfile.

gem 'mailgun-ruby'

Then run bundle install command to deploy the gem.

Change the API and MailPlugger.plug_in method in config/initializers/mail_plugger.rb.

class MailgunApiClient
  def initialize(options = {})
    @settings = { api_key: ENV['MAILGUN_API_KEY'] }
    @options = options
  end

  def deliver
    Mailgun::Client.new(@settings[:api_key]).send_message('sending_domain.com', generate_mail_hash)
  end

  private

  def generate_mail_hash
    {
      from: @options[:from].first,
      to: @options[:to].join(','),
      subject: @options[:subject],
      text: @options[:text_part],
      html: @options[:html_part]
    }
  end
end

MailPlugger.plug_in('mailgun') do |api|
  api.client = MailgunApiClient
  api.delivery_options = %i[from to subject text_part html_part]
  api.delivery_settings = { return_response: true }
end

Then modify the mailer method a little bit.

class TestMailer < ApplicationMailer
  default from: 'from@example.com'

  def send_test
    mail subject: 'Test email', to: 'to@example.com', delivery_system: 'mailgun'
  end
end