ruby-on-railsemailgmailamazon-sescontent-encoding

Content-Transfer-Encoding: quoted-printable is breaks the break lines


I´m sending emails through a Rails web app using AmazonSes service. I´m sending different types of emails and everything is working fine, but in the next scenario:

The user can write a text like a message with line breaks in a textarea field within a form. This message is stored in our MySQL dababase and sent by email to another user. I have problems with the line breaks, because depending on if I open the email in Gmail webmail or Mac Mail, the break lines are respected or not. I have read a lot of post with similar problem and tried but unfortunately I couldn´t fix it.

As mentioned, I´m using Rails Mailer to send the emails through AmazonSES services.

  1. Input textarea with message: enter image description here

This is what I store from the textarea field in database: "messagePreOffer"=>"This is paragraph 2.\n\nThis is paragraph 2.\n\nThis is paragraph 3."

  1. Before rendering the mail template the messagePreOffer log shows:

    offer.message_pre_offer=This is paragraph 1.

    This is paragraph 2.

    This is paragraph 3.

  2. Simplified mail template layout:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <!-- NAME: 1:2 COLUMN -->
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <p><%= @offer.message_pre_offer %></p>
    </body>
    </html>
    
  3. We send the email with mailer (I think this is not relevant):

     mail(from: APP_CONFIG['EMAIL_FROM'], to: offer.email, bcc: APP_CONFIG['EMAIL_FROM'], subject: t('new_offer_user_subject', departure: @offer.enquire.departure))
    
  4. Mailer delivery configuration:

     config.action_mailer.delivery_method = :smtp
     config.action_mailer.smtp_settings = {
         :address => "email-smtp.eu-west-1.amazonaws.com",
         :port => 587,
         ...
         :authentication => :login,
         :enable_starttls_auto => true
     } 
    

TEST 1: This is the log that I got after running and sending the email:

Rendering email_service/send_new_offer_user_notification.html.erb within layouts/email_service
  Rendered email_service/send_new_offer_user_notification.html.erb within layouts/email_service (2.0ms)
EmailService#send_new_offer_user_notification: processed outbound mail in 75.5ms
Sent mail to *****@gmail.com (1117.1ms)
Date: Wed, 16 Dec 2020 17:56:23 +0100
From: *******
To: *****@gmail.com
Message-ID: <5fda3c373232d_42433fc7916a717c9102b@MacBook-Pro-****.local.mail>
Subject: Whatever
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5fda3c3731200_42433fc7916a717c909a4";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_5fda3c3731200_42433fc7916a717c909a4
Content-Type: text/plain;
 **charset=UTF-8
Content-Transfer-Encoding: 7bit**

This is paragraph 1.

This is paragraph 2.

This is paragraph 3.
----==_mimepart_5fda3c3731200_42433fc7916a717c909a4
Content-Type: text/html;
 **charset=UTF-8
Content-Transfer-Encoding: 7bit**

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- NAME: 1:2 COLUMN -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<p>This is paragraph 1.

This is paragraph 2.

This is paragraph 3.</p>
</body>
</html>

----==_mimepart_5fda3c3731200_42433fc7916a717c909a4--

With this initial configuration, I get the next results:

I understand this is right, because I didn´t add any format.

TEST 2: While making tests to write this post I went one step further. My second test just added the next css, to keep line breaks:

<p style="white-space: pre-line;"><%= @offer.message_pre_offer %></p>

After running the test, I got very surprises that it worked perfectly in both Gmail webmail and Mac mail. Find screen here: enter image description here

TEST 3: The lines used in the previous test where just for testing purposes. As the tests worked, I tried to use the real text template in spanish. Surprisingly the test didn´t work.

This is the variable log:

"messagePreOffer"=>"Este es el párrafo 1.\n\nEste es el párrafo 2.\n\nEste es el párrafo 3."

This is the log that I got after running and sending the email:

Sent mail to ***@gmail.com (1130.8ms)
Date: Wed, 16 Dec 2020 18:53:23 +0100
From: *****
To: *****
Message-ID: <5fda499338a0f_42433fc7916a717c946d1@MacBook-Pro-de-******.local.mail>
Subject: Whatever
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5fda49933789a_42433fc7916a717c945df";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_5fda49933789a_42433fc7916a717c945df
Content-Type: text/plain;
 charset=UTF-8
**Content-Transfer-Encoding: quoted-printable**

Este es el p=C3=A1rrafo 1.=0D
=0D
Este es el p=C3=A1rrafo 2.=0D
=0D
Este es el p=C3=A1rrafo 3.=

----==_mimepart_5fda49933789a_42433fc7916a717c945df
Content-Type: text/html;
 charset=UTF-8
**Content-Transfer-Encoding: quoted-printable**

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.o=
rg/TR/xhtml1/DTD/xhtml1-strict.dtd">=0D
<html xmlns=3D"http://www.w3.org/1999/xhtml" xmlns=3D"http://www.w3.org/1=
999/xhtml">=0D
<head>=0D
<!-- NAME: 1:2 COLUMN -->=0D
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8" =
/>=0D
<meta name=3D"viewport" content=3D"width=3Ddevice-width, initial-scale=3D=
1.0" />=0D
</head>=0D
<body>=0D
<p style=3D"white-space: pre-line;">Este es el p=C3=A1rrafo 1.=0D
=0D
Este es el p=C3=A1rrafo 2.=0D
=0D
Este es el p=C3=A1rrafo 3.</p>=0D
</body>=0D
</html>=0D

----==_mimepart_5fda49933789a_42433fc7916a717c945df--

Then in Mail Mac the format is still ok: enter image description here

However, in Gmail webmail the break lines are broken, with many lines and much more space: enter image description here

After some tests with the content text, I have found out that the problem starts with the accents. If you take a deep look into the logs, in the first test the charset is:

    charset=UTF-8
    Content-Transfer-Encoding: 7bit 

However, as soon as I include an accent in my text, the content transfer encoding automatically changes to:

    charset=UTF-8
    **Content-Transfer-Encoding: quoted-printable**

This is the root cause why the break lines are broken. Actually, If I just remove the accent, everything works in Gmail again: enter image description here

Now, I found the root cause of the problem, but I´m stuck figuring out which is the right solution.


Solution

  • After much more investigation I found this other interesting post: Why isn't Gmail using quoted-printable encoding?

    that pointed me that Gmail is not working with quoted-printable. So, I have fixed the problem by forcing the mailer to use 7-bit transfer enconding, with next line in my mailer:

    default 'Content-Transfer-Encoding' => '7bit'