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

Closed
kelsin opened this Issue Apr 26, 2011 · 17 comments

9 participants

@kelsin

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.

Thanks,
Chris

@minad

Unfortunately this isn't possible with the current implementation.

@kelsin

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
@felipecsl

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.

Thanks!

@kelsin

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: https://twitter.com/kelsin5/status/225069263982047234

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.

@felipecsl

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

@bploetz

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

html
  head
    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.

@kelsin

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 :)

@felipecsl

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

@minad

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

@patricklewis

@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!

@amxn

@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!

@adsteel

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?

@kelsin

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.

http://en.wikipedia.org/wiki/Quoted-printable

@adsteel

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

@kelsin

😸

@arxpoetica

This thread just saved my bacon.

@softcraft-development

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:

https://gist.github.com/softcraft-development/79152aba2d23690e5cd1

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
Merged

Forcing UTF-8 rendering #1

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