Rails 4.1 Force ActionMailer to return a different value

how can I get it to return a different value in the ActionMailer method. Example:

class TestMailer < ActionMailer::Base

  extend MandrillApi

  def index(email, code)
    @code = code
    result = MandrillApi.send({subject: t('test.index.subject'), to: email, template: render_to_string})
    return result


In this case, I am using ActionMailer to render the template (render_to_string) and pass variables to the view, but I need to get the output from the MandrillApi module. When I call the method TestMailer.index("xxxx@gmail.com", "asdf123")

, the return value is#<ActionMailer::Base::NullMail:0x007fe5c433ab10>



source to share

1 answer

Create a separate class (not a descendant ActionMailer::Base

) for this task. ActionMailer will overwrite return values ​​from its mailer actions.

You can use render_anywhere gem as solution:

require 'render_anywhere'

class MandrilMailer
  extend MandrillApi
  include RenderAnywhere

  # We need that for 't' i18n helper to work
  include ActionView::Helpers::TranslationHelper

  class RenderingController < RenderAnywhere::RenderingController
    # we can use that for url_helpers to work properly
    def default_url_options
      host = {'production' => 'production.example.org', 'development' => 'development.example.org'}[Rails.env]
      {host: host}
    # alternatively, you can add this line inside you config/environments/*.rb files, setting your own host:
    # Rails.application.routes.default_url_options[:host] = 'example.org'
  def index(email, code)
    # 'render' is a RenderAnywhere call
    MandrilApi.send({subject: t('test.index.subject'), to: email, template: render(template: 'test_mailer/index', locals: {code: code}, layout: false)})

MandrilMailer.new.index("user@example.org", "asdf123") # => result of MandrilApi.send




All Articles