Is there a way to set options per controller/template? #123

kelsin opened this Issue Apr 26, 2011 · 17 comments

9 participants


We need to turn pretty printing on JUST for mailer views in rails. I'm unfortunately not too familiar with the whole rails template stack (reading about it now) but wanted to find out if there is anything known about how to edit options per view/controller instead of just setting the default.



Unfortunately this isn't possible with the current implementation.


We actually made rails force quoted-printable encoding which forces line wraps. Our issue was in email templates SendGrid was breaking html by inserting line breaks in very stupid places.

By forcing quoted-printable encoding it line breaks every 70 chars and we don't need to pretty print slim. But that's for your answer!

@kelsin kelsin closed this Apr 27, 2011

Hey @Kelsin, I think we're having a similar problem with rails + slim + sendgrid, where emails are getting line breaks at weird places, like in the middle of the tag name, for example, causing the email to be rendered correctly. I was unsure if this was caused by slim or sendgrid, but as per what you said earlier, it is sendgrid's fault. Can you point to the right way to fix this please? I can't figure how to enable slim to wrap lines just for this or to avoid these stupid line breaks.



I don't have access to the code where I made the change anymore but I'm asking a former co-worker if he can gist it up for you:

It was sendgrid's fault but clearly we couldn't change that unfortunately, and (as you can see in this issue) we couldn't make slim pretty print JUST this mailer or controller. What we ended up doing is making sure that rails would encode all of our emails with quoted printable encoding before sending them to sendgrid. I'm pretty sure it was just an override of some rails code. If I get a tweet back I'll post the fix.


Thanks a lot @Kelsin, hopefully your ex co-worker can help me out :)


@Kelsin @felipecsl I'm pretty sure this is all we did to get this to work:

    meta content="text/html; charset=UTF-8" http-equiv="Content-Type"
  body data-force-encoding="✓"

Not sure if the meta tag matters or not, but the body data-force-encoding for sure does.


Oh yeah, I remember that now, just having the UTF character made rails use quote printable encoding making sure that even with non-pretty printed slim, sendgrid did the right thing :)

Thanks Brian :)


@bploetz @Kelsin Thanks guys, worked perfectly :))


Please ask on the rails mailing list on howto set options per rendering or per controller. I wondered often why this is not possible.


@bploetz Your fix of adding body data-force-encoding="✓" to my Slim email template just solved a problem I've been up against for two days with a listserv inserting random whitespace and breaking links in my HTML emails. It was a line length issue and quoted-printable totally solved that by breaking lines at column 74.

Thanks so much!


@bploetz So after all the nasty hacks and hand-encoding special characters I realized it was my Slim layout template that was breaking encoding for emails. (We have a horrific Liquid-RedCloth-Slim render chain for email templates)

Thanks a ton!


When we add body data-force-encoding="✓", an equals sign is added at the end of each line break, breaking links that happen to fall on the line break. Here's an example:

 </head><body data-force-encoding=3D"=E2=9C=93"><table align=3D"center" ce=
 llpadding=3D"0" cellspacing=3D"0" class=3D"container" width=3D"600px"><tr=
 class=3D"header" style=3D"background-color: #3cd929;"><td style=3D"paddi=

Anyone else encountered this?


Yes, isn't that what you want? When you add the UTF8 character to your layout you're forcing rails to encode your emails as quoted-printable. Your emails are encoded, it's not plain text. The = is a escape character making sure that the actual output doesn't include the line break. In our original problem, this caused send grid to not need to line break anymore since quated-printable makes sure to have 76 character lines.


Yep, you're right. Sorry, asked too soon. We're dealing with a separate issue, turns out. Thanks for responding.




This thread just saved my bacon.


data-force-encoding works well enough for HTML when you can slip a UTF-8 (ie: non-US-ASCII) character in without impact. However, I've come up with a way to explicitly set the Content-Transfer-Encoding header on the mail parts without changing the content. (This techniques works well on plain-text templates in addition to Slim templates).

The code in ActionMailer::Base that creates email messages/parts from mailer templates is pretty straightforward, and protected, so it's relatively easy to customize. The method I chose is to extend insert_part to set the Content-Transfer-Encoding field to quoted-printable on each Mail:Part created for each template, like so:

I did this in my ApplicationMailer, but it could certainly be applied on a per-mailer basis, according to your needs.

Hope this helps!

@joeadcock joeadcock referenced this issue in RoguePod/mailpeek Feb 9, 2016

Forcing UTF-8 rendering #1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment