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 = ‘[email protected]’
@from = ‘[email protected]’
@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/<%= @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.