How to apply a specific "view" when I send out an *email*


#1

Hi there,

how can I tell my Rails app Mailer to:

-> Apply a specific “view” when I send out an email ?

When I don’t specify anything specific, my “WhateverMailer” looks for
its “view” (.erb file) as if the Mailer was a Controller (following the
same standard logic: …/app/views/ directory).

But I need my WhateverMailer to go find the .erb file on a different
path - but how and where to tell it to??

Thanks a lot for any help!
Tom


#2

On Wed, Mar 4, 2009 at 4:17 PM, Tom Ha
removed_email_address@domain.invalid wrote:

how can I tell my Rails app Mailer to:

-> Apply a specific “view” when I send out an email ?

The trick is

body       render_message(channel, "")

in app/models/…_mailer.rb (example below).

So, a few hints from my code (changed some parts for censuring
the internal business logic, so may be inexact, sorry upfront for that)

In the app/controllers/mail_controller.rb

def handle_email

case preview_or_send
when :preview
@email = ChannelMailer.create_channel_mail(details, @channel)
when :send
delivery_state = “OK”
begin
@email = ChannelMailer.deliver_channel_mail(details, @channel)
rescue
delivery_state = “FAIL”
logger.info “FAILED mail delivery, email = #{@email}”
end

fill_log_entry @email, delivery_state, details, @channel
end

end

In app/models/channel_mailer.rb
class ChannelMailer < ActionMailer::Base
def channel_mail(details, channel)
subject “censured”
from FROM
sent_on Time.now

case channel
when "censured"
    to_list = ...
    bcc_list = ...
...
end
set_recipients(to_list, bcc_list)
body       render_message(channel, "") # ###### This is the real

answer to your question

end

end

In app/view/channel_mailer/
$ ls -1
_details_for_channel.erb
channel_1.erb
channel_2.erb
channel_3.erb

HTH,

Peter


#3

Very cool - thanks a lot, Peter!