# Ruby on Rails mailer

**URL:** <https://www.ruby-forum.com/t/ruby-on-rails-mailer/225982>\
**Category:** Rails\
**Created:** [December 20, 2007, 8:41am UTC](https://www.ruby-forum.com/t/ruby-on-rails-mailer/225982 "2007-12-20T08:41:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![franz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c37758/32.png) [@franz](https://www.ruby-forum.com/u/franz)\
**Post date:** [December 20, 2007, 8:41am UTC](https://www.ruby-forum.com/t/ruby-on-rails-mailer/225982/1 "2007-12-20T08:41:56Z")

</div>

Today I made a first attempt to make use of ruby mailer. A volunteer  
fills out a form and the information is then sent to an administrator.

I generated a mailer called notifier and added the following:

class Notifier \< ActionMailer::Base  
def volunteer\_signup(volunteer, sent\_at = Time.now)  
@subject = ‘A new volunteer has signed up’  
@body = { :title =\> volunteer.title, :first\_name =\>  
volunteer.first\_name, :last\_name =\> volunteer.last\_name,  
:id =\> volunteer.id }  
@recipients = ‘removed\_email\_address@domain.invalid’  
@from = ‘removed\_email\_address@domain.invalid’  
@sent\_on = sent\_at  
@headers = {}  
@content\_type = ‘text/html’  
end  
end

Then I generated a view called volunteer\_signup.html.erb

Dear xxx,

\<%= @title %\>\<%= @first\_name %\> \<%= @last\_name %\> has signed up as a  
new volunteer.

To see his/her information copy the text below into your browser’s  
address bar.  
[http://localhost:3000/volunteers/](http://localhost:3000/volunteers/)\<%= @id %\>

I added the following line to my volunteer\_controller create method:

Notifier.deliver\_volunteer\_signup(@volunteer)

I ran the code and got the following error:

mailer sub type missing: “html”

I ran the debugger and noticed the following;

At /usr/lib/ruby/gems/1.8/gems/actionmailer-2.0.1/lib/action\_mailer/  
part.rb:59:in ‘to\_mail’

59: real\_content\_type, ctype\_attrs = parse\_content\_type(defaults)

This method takes the content\_type of the message “text/html” and  
splits the string so that variable real\_content\_type is “html”. But  
then on line 82 it calls;

82: part.set\_content\_type(real\_content\_type, nil, ctype\_attrs)

def set\_content\_type( str, sub = nil, param = nil )  
if sub  
main, sub = str, sub  
else  
main, sub = str.split(%r\</\>, 2)  
raise ArgumentError, “sub type missing: #{str.inspect}” unless  
sub  
end  
if h = @header[‘content-type’]  
h.main\_type = main  
h.sub\_type = sub  
h.params.clear  
else  
store ‘Content-Type’, “#{main}/#{sub}”  
end  
@header[‘content-type’].params.replace param if param  
str  
end

As you can str contains “html” as real\_content\_type was passed in, sub  
is nil. It then tries to split this string again, which has been  
split before. So str will be split into “html” and nil. Since sub is  
nil, it will raise the error. If I leave @content\_type blank, the  
same error is raised.

Could this be a bug in mailer? If you change str or real\_content\_type  
to “text/html” in the debugger, the code will run without errors. I’m  
new to Ruby on rails, so if this is a bug, please let me know where I  
should submit it.

---

<div class="post-metadata">

**Author:** ![franz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c37758/32.png) [@franz](https://www.ruby-forum.com/u/franz)\
**Post date:** [December 23, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-mailer/225982/2 "2007-12-23T21:35:36Z")

</div>

Are you using the globalize plugin? Apparently if you comment out  
‘require “globalize/rails/action\_mailer”’ in vendor/plugins/globalize/  
init.rb the error goes away… or maybe try updating the plugin if they  
fixed the bug yet…

---

<div class="post-metadata">

**Author:** ![franz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c37758/32.png) [@franz](https://www.ruby-forum.com/u/franz)\
**Post date:** [January 19, 2008, 4:48am UTC](https://www.ruby-forum.com/t/ruby-on-rails-mailer/225982/3 "2008-01-19T04:48:50Z")

</div>

I tried updating the plugin but bug isn’t fixed. Your suggestion  
works though.

---

<div class="post-metadata">

**Author:** ![franz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c37758/32.png) [@franz](https://www.ruby-forum.com/u/franz)\
**Post date:** [January 27, 2008, 10:55am UTC](https://www.ruby-forum.com/t/ruby-on-rails-mailer/225982/4 "2008-01-27T10:55:44Z")

</div>

Hi Franz,  
instead of commenting out the globalize/rails/action\_mailer and  
disabling the ability to send Mails in different languages hereby, you  
should extend your email templates with the locale parameter. For  
example english templates should look like this:

signup\_notification.en.text.plain.erb  
signup\_notification.en.text.html.erb

Globalize overrides the Action Mailers create-Method giving it the  
ability to check for multilanguage extentions, as well as the new  
Action Mailer(2.0.2) is checking for multipart email extentions.  
So there is no bug anywhere. Everything will work fine.

For more information read the Rails API, section Multipart email of  
ActionMailer::Base Class

> **[ActionMailer::Base](https://api.rubyonrails.org/classes/ActionMailer/Base.html)**
>
> Action Mailer allows you to send email from your application using a mailer model and views. Mailer Models To use Action Mailer, you need to create a mailer model.

Best wishes,  
Roman

---

<div class="post-metadata">

**Author:** ![franz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c37758/32.png) [@franz](https://www.ruby-forum.com/u/franz)\
**Post date:** [January 28, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-mailer/225982/5 "2008-01-28T19:40:29Z")

</div>

Franz,

Roman’s answer hits the nail on the head. I had the same problem and  
this is exactly what I also concluded. Follow naming convention for  
localized mailer templates:

\<mailer\_method\>.[.]\<mime\_type\>..erb

Ex.: activation.en-US.text.html.erb

## Docs (vendor/plugins/globalize/lib/globalize/rails/action\_mailer.rb)

Globalize overrides the create! method to support multiple templates  
for different locales.  
For example, for English it will select the template:

```
signup_notification.en-US.text.html.rhtml

```

It will look for the currently active locale code (en-US) first, then  
the language code (en).

If neither of those are found, it will use the regular name:

```
signup_notification.text.html.rhtml

```

It is fully backwards compatible with the original Rails version.

Cheers, Sazima
