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

**URL:** <https://www.ruby-forum.com/t/how-to-apply-a-specific-view-when-i-send-out-an-email/161988>\
**Category:** Rails\
**Created:** [March 4, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/how-to-apply-a-specific-view-when-i-send-out-an-email/161988 "2009-03-04T16:17:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 4, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/how-to-apply-a-specific-view-when-i-send-out-an-email/161988/1 "2009-03-04T16:17:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 4, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-apply-a-specific-view-when-i-send-out-an-email/161988/2 "2009-03-04T17:03:23Z")

</div>

On Wed, Mar 4, 2009 at 4:17 PM, Tom Ha  
[removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 4, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/how-to-apply-a-specific-view-when-i-send-out-an-email/161988/3 "2009-03-04T17:28:26Z")

</div>

Very cool - thanks a lot, Peter!
