# ActionMailer - ROR Recipes - Multipart/alternative

**URL:** <https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703>\
**Category:** Rails\
**Created:** [February 6, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703 "2006-02-06T22:01:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 6, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703/1 "2006-02-06T22:01:12Z")

</div>

I am using the SaltedHashLoginGenerator. and would like to send the  
e-mail in multipart/alternative format.

By default my application is using forgot\_password\_en.rhtml template.

I read in the ROR Recipes book

“ActionMailer sees these templates, recognizes the pattern in their  
file names, and automatically sets the MIME type of the message to  
multipart/alternative and adds each rendered view with the content  
type  
in its file name.”

So I created forgot\_password\_en.text.plain.rhtml template and  
forgot\_password\_en.text.html.rhtml templates (and removed the  
forgot\_password\_en.rhtml template temporarily).

Now my application won’t send any email.

How can I make ActionMailer recognize these templates and set the  
appropriate MIMe type?

My mailer function looks like this:

```
def forgot_password(member, url=nil)
  setup_email(member)

  # Email header info
  @subject += "Forgotten password notification"

  # Email body substitutions
  @body["name"] = "#{member.firstname} #{member.lastname}"
  @body["login"] = member.login
  @body["url"] = url || MemberSystem::CONFIG[:app_url].to_s
  @body["app_name"] = MemberSystem::CONFIG[:app_name].to_s
end

```

Thanks for your assistance.  
Frank

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 7, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703/2 "2006-02-07T16:42:57Z")

</div>

On 2/6/06, softwareengineer 99 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> multipart/alternative and adds each rendered view with the content type
> 
> ```
> @body["name"] = "#{member.firstname} #{member.lastname}"
> @body["login"] = member.login
> @body["url"] = url || MemberSystem::CONFIG[:app_url].to_s
> @body["app_name"] = MemberSystem::CONFIG[:app_name].to_s
> 
> ```
> 
> end

Hi Frank.

Would this work if your templates weren’t called “\_en”? I believe  
ActionMailer requires the template to be named _exactly_ the same as the  
mailer’s method (forgot\_password) with the remaining dot-separated parts  
of  
the name representing the mime type and template type (eg .rhtml).

So if you rename your templates to, for example,  
forgot\_password.text.plain.rhtml does your mailer start working again?

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India, and  
All  
I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 7, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703/3 "2006-02-07T19:13:04Z")

</div>

Chad F. wrote:

> Would this work if your templates weren’t called “\_en”? I believe  
> ActionMailer requires the template to be named _exactly_ the same as the  
> mailer’s method (forgot\_password) with the remaining dot-separated parts  
> of the name representing the mime type and template type (eg .rhtml).
> 
> So if you rename your templates to, for example,  
> forgot\_password.text.plain.rhtml does your mailer start working again?

I used SHLG and it generated a “forgot\_password” method in my  
MemberNotify ActionMailer  
subclass and a “forgot\_password\_en.html” view. That all appears to work.

Sorry I can’t offer any insight on why things aren’t working – I’m  
still very new to all  
this – but I thought I should point out that the “\_en” isn’t breaking  
mine. I’m fairly  
certain that’s added by the Localization plugin.

b

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 7, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703/4 "2006-02-07T17:20:33Z")

</div>

Hello Chad,  
Thank you for your reply. I will certainly follow your tip and post  
the results here. It sounds like it should work, but I will let you  
know.

Thanks  
Frank

Chad F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Hi Frank.

Would this work if your templates weren’t called “\_en”? I believe  
ActionMailer requires the template to be named _exactly_ the same as  
the mailer’s method (forgot\_password) with the remaining dot-separated  
parts of the name representing the mime type and template type (eg  
.rhtml).

So if you rename your templates to, for example,  
forgot\_password.text.plain.rhtml does your mailer start working again?

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India,  
and All I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 7, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703/5 "2006-02-07T19:25:32Z")

</div>

On 2/7/06, Ben M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I used SHLG and it generated a “forgot\_password” method in my MemberNotify ActionMailer  
> subclass and a “forgot\_password\_en.html” view. That all appears to work.
> 
> Sorry I can’t offer any insight on why things aren’t working – I’m still very new to all  
> this – but I thought I should point out that the “\_en” isn’t breaking mine. I’m fairly  
> certain that’s added by the Localization plugin.

Yea, my bet is (I don’t know the localization plugin well) that the  
localization plugin overrides the view-finding logic in a way that  
doesn’t affect the hardcoded checks that happen in the ActionMailer  
code. It might be worth a patch to ActionMailer to make it more  
flexible if that’s the case (or maybe not).

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India,  
and All I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 7, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703/6 "2006-02-07T20:16:44Z")

</div>

On 2/7/06, softwareengineer 99 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> EOL
> 
> I commented the entire code out, restarted the server and tried again and  
> voila!, it worked.

I’m not surprised. Thanks for posting your findings.

> figure it out)  
> 3. How to specify content type of text/html without using multipart for  
> SLHG (by using @content\_type=“text/html”)

Thanks for your suggestions.

> The SLHG works great, though it does takes some time. I have finally  
> integrated it and recommend that it be added to the recipes also. The  
> documentation that comes with SLHG is pretty self explanatory but can be  
> overwhelming.
> 
> What editor did you use to compose the book? Docbook or something else?

I’m using the Pragmatic Programmers’ proprietary (and absolutely  
amazing!) publishing system. All of the writing is done in XML.

> Thank you once again for your assistance and great tips.

Thanks!

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India,  
and All I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 7, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/actionmailer-ror-recipes-multipart-alternative/101703/7 "2006-02-07T20:07:36Z")

</div>

Dear Chad,

Thank you for your assistance.

I tried renaming as per your suggestion and tried resending email but  
the email delivery failed.

Then I looked in the localization code and found the following code in  
lib/localization.rb which is responsible for adding the \_en to the  
template name:

```
# redefinition of ActionMail::Base#render_message, that adds locale 

```

suffix to  
# the template name  
ActionMailer::Base.module\_eval \<\<-EOL  
private  
def render\_message(method\_name, body)  
initialize\_template\_class(body).render\_file(method\_name +  
“\_#{CONFIG[:default\_language]}”)  
end  
EOL

I commented the entire code out, restarted the server and tried again  
and voila!, it worked.

My forgot password message arrived with content-type set to:

multipart/alternative;  
boundary=mimepart\_43e8db39b433c\_1a4a…fdbaf1bcc3c9

Seeing how many folks are using SLHG, I highly recommend the addition  
of following tips as the addition of these will highly benefit your  
readers:

1. How to modify the above code to keep the localization but still be  
able to use multi-part messages.
2. How to specify a reply\_to address for ActionMailer. (I still cannot  
figure it out)
3. How to specify content type of text/html without using multipart  
for SLHG (by using @content\_type=“text/html”)

The SLHG works great, though it does takes some time. I have finally  
integrated it and recommend that it be added to the recipes also. The  
documentation that comes with SLHG is pretty self explanatory but can  
be overwhelming.

What editor did you use to compose the book? Docbook or something  
else?

Thank you once again for your assistance and great tips.

Frank  
Ruby Gemologist  
P.S. I recommend that all Ruby developers be officially called Ruby  
gemologists to stand out from the rest.

Chad F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: On 2/7/06, Ben M. wrote:

> I used SHLG and it generated a “forgot\_password” method in my MemberNotify ActionMailer  
> subclass and a “forgot\_password\_en.html” view. That all appears to work.
> 
> Sorry I can’t offer any insight on why things aren’t working – I’m still very new to all  
> this – but I thought I should point out that the “\_en” isn’t breaking mine. I’m fairly  
> certain that’s added by the Localization plugin.

Yea, my bet is (I don’t know the localization plugin well) that the  
localization plugin overrides the view-finding logic in a way that  
doesn’t affect the hardcoded checks that happen in the ActionMailer  
code. It might be worth a patch to ActionMailer to make it more  
flexible if that’s the case (or maybe not).

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India,  
and All I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)
