Ben Reubenstein
2006-Nov-04 02:07 UTC
[Rails] Re: ActionMailer Sending Two text/plain Parts
Rails once again is smarter than me. However it sure did a good job
of covering it up. My issue is if I used the render_message to
specify the template file to use, the Content-Type of the email was
being set to multipart/mixed instead of multipart/alternative. I
found a creative but very wrong way to fix this by specifying a
default_content_type in my environment.rb file, which fixed my one
email problem, then broke all of my text/plain only emails.
I opened up the rails/actionmailer/lib/actionmailer/base.rb and found
that there is a :template variable that you can set, which states:
# Specify the template name to use for current message. This is the
"base"
# template name, without the extension or directory, and may be used to
# have multiple mailer methods share the same template.
It should also say, "or allow one mailer method to use multiple
templates". This is not in the API docs (along with a few other
goodies), so I will try to help get a fix in for this.
So to finish things off I set the value of :template and the rails
magic grabbed the correct types based on the file names
(text.html.rhtml, text.plain.rhtml, etc...). My action ended up
looking much DRY''er.
def newsletter(email_address, subject, template_to_use)
@from = "no-reply@nothing.com"
@subject = subject
@recipients = email_address
@template = template_to_use
end
Hope this helps someone else.
Cheers,
Ben
On 8/30/06, Ben Reubenstein <benr@x-cr.com> wrote:> Hello ~
>
> I have a working ActionMailer Action that takes the template to use as
> an argument so I have one action that handles multiple emails. The
> email sends just fine, and the correct email templates are used, but
> ActionMailer is throwing in its own text/plain section in addition to
> the one I specify. Does anyone have any ideas on why this might be
> occuring, when I am specificially specifying a "text/plain" part?
>
> Thx in advance, here is the code:
>
> notifier.rb code
> ------------------
> def dynamic_mailer(email_address, subject, template_to_use)
> @from = "no-reply@nothing.com"
> @subject = subject
> @recipients = email_address
>
> part :content_type => "text/plain", :body =>
> render_message(template_to_use + ".text.plain.rhtml", :foo =>
"foo")
>
> part :content_type => "text/html", :body =>
> render_message(template_to_use + ".text.html.rhtml", :foo =>
"foo")
> end
>
> resulting email contains two text/plain sections
> ------------------
> --mimepart_44f5dbaadb3d0_b20127aae466a6
> Content-Type: text/plain
> Content-Transfer-Encoding: Quoted-printable
> Content-Disposition: inline
>
>
> --mimepart_44f5dbaadb3d0_b20127aae466a6
> Content-Type: text/plain; charset=utf-8
> Content-Transfer-Encoding: Quoted-printable
> Content-Disposition: inline
>
> This is an HTML Rich Email. You can view it online here:
>
> http://www.someurl.com/somefile
>
> --mimepart_44f5dbaadb3d0_b20127aae466a6
> Content-Type: text/html; charset=utf-8
> Content-Transfer-Encoding: Quoted-printable
> Content-Disposition: inline
>
> <html>
> <head>
> </head>....
>
> --
> Ben Reubenstein
> 303-947-0446
> http://www.benr75.com
>
--
Ben Reubenstein
303-947-0446
http://www.benr75.com
Hello ~
I have a working ActionMailer Action that takes the template to use as
an argument so I have one action that handles multiple emails. The
email sends just fine, and the correct email templates are used, but
ActionMailer is throwing in its own text/plain section in addition to
the one I specify. Does anyone have any ideas on why this might be
occuring, when I am specificially specifying a "text/plain" part?
Thx in advance, here is the code:
notifier.rb code
------------------
def dynamic_mailer(email_address, subject, template_to_use)
@from = "no-reply@nothing.com"
@subject = subject
@recipients = email_address
part :content_type => "text/plain", :body =>
render_message(template_to_use + ".text.plain.rhtml", :foo =>
"foo")
part :content_type => "text/html", :body =>
render_message(template_to_use + ".text.html.rhtml", :foo =>
"foo")
end
resulting email contains two text/plain sections
------------------
--mimepart_44f5dbaadb3d0_b20127aae466a6
Content-Type: text/plain
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline
--mimepart_44f5dbaadb3d0_b20127aae466a6
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline
This is an HTML Rich Email. You can view it online here:
http://www.someurl.com/somefile
--mimepart_44f5dbaadb3d0_b20127aae466a6
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline
<html>
<head>
</head>....
--
Ben Reubenstein
303-947-0446
http://www.benr75.com