8

The 'Content-Transfer-Encoding' setting is set to '7bit' by default. The mail server Postfix is breaking down the email header by bunch of 1000 caracteres, meaning that if you have a long email (using HTML for example), you end up having spaces in the middle of your text or links. (See this thread for more info: http://tech.groups.yahoo.com/group/postfix-users/message/273296)

Following the Rails ActionMailer documentation (http://api.rubyonrails.org/classes/ActionMailer/Base.html), adding the following code to my app file should make it, but it doesn't work:

ActionMailer::Base.default 'Content-Transfer-Encoding' => 'quoted-printable'

I still end up with the default:

Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_50166adf1e043_1b9810829142282d";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

My email look like that:

def new_registered_user(user_id) 
    @user = User.find(user_id)

    set_locale @user.locale

    mail(
      :subject => i18n_subject,
      :to => @user.email_with_name
    ) do |format|
      format.text { render :layout => 'text_email' }
      format.html
    end
  end

Any idea on what else should I change?

Vincent Peres
  • 894
  • 11
  • 26

3 Answers3

9

I found that setting the (undocumented) transport_encoding on the Mail object works:

m = mail(...)
m.transport_encoding = "quoted-printable"
m.deliver

I could not get the documented approach of setting the Content-Transfer-Encoding via ActionMailer to work.

My environment: rails (3.1), mail (~> 2.3.3)

Felix Livni
  • 992
  • 12
  • 21
  • fyi, email line length is limited to 998 characters. see http://stackoverflow.com/questions/1592291/what-is-the-email-subject-length-limit – Felix Livni Jan 23 '13 at 23:31
  • thank you very much, so there is no other option than adding breakline (\r\n) for long emails – Vincent Peres Mar 11 '13 at 11:56
  • Indeed. There MUST be a CRLF every 998 characters. That said, depending on your content encoding, you can avoid putting the CRLF in the content itself. – Felix Livni Mar 22 '13 at 04:25
  • Thanks so much! for question and answer.. what a tricky problem this was! Instead of `m.transport_encoding = "quoted-printable"` I use `m.transport_encoding = "base64"` because we have to send mail in foreign chars and according to wikipedia: "Base64 ... is the more sensible choice for binary formats or text in non-Latin based languages." from https://en.wikipedia.org/wiki/Quoted-printable – ryan2johnson9 Dec 09 '15 at 02:40
  • p.s. I was lucky to find this question. I had the same problem of spaces in my text and links but had no idea that the content_transfer_encoding had to be altered. For the benefit of others it might be good to add in the title "Rails random spaces in email text and links" or something.. or maybe this comment alone will help the search results. – ryan2johnson9 Dec 09 '15 at 02:44
3

If you use a custom mailer class, you can set it as a default option in your mailer class, as described in the documentation of ActionMailer::Base

class Notifier < ApplicationMailer
  default 'Content-Transfer-Encoding' => '7bit'
end
0x4a6f4672
  • 24,450
  • 15
  • 96
  • 130
0

I also ran into this problem recently. Rails was defaulting to quoted-printable, but that was causing rendering problems for me in a webmail client. New lines were rendered as =0D, which the webmail client rendered incorrectly.

My solution to this was the alter the content encoding of only the html part since I'm also sending a separate text part and do not want to change its encoding. The html method takes a content_transfer_encoding argument, avoiding the need for a temporary variable:

mail do |format|
  format.text { ... }
  format.html(content_transfer_encoding: '7bit') { ... }
end

The documentation references using default 'Content-Transfer-Encoding' ... but that only works when not calling the mail method directly.

Aaron Breckenridge
  • 1,607
  • 18
  • 24