2

I have some difficulties sending quoted-printable encoded HTML mails properly. My HTML needs to be cut into lines to fullfil the requirement of having no line of more than 79 characters that is required for compatibility reasons - I have found the concept of a "soft line break" for this usecase.

A working example of this is:

( mailto:abc@someonesdomain.tld?subject=3DReply%20to%20this%20Post%20%5B428=
2-8596-332127136989972_339826386220047-6307b30ea80af240f0557f2340d9758d%5D&=
body=3D******%20Enter%20your%20reply%20below%20this%20line%20and%20hit%20SE=
ND.%20You%20may%20also%20attach%20an%20image%20to%20your%20reply%20****** )

When I copy the line ends to notepad it shows only = CR LF.

So I've tried to use wordwrap($newHtmlBody, 70, '='.PHP_EOL, true); and my output kinda looked like working example above. However, when viewing the sent email in gmail, it would not accept this. What am I doing wrong?

The relevant email header/body above the HTML content is:

MIME-Version: 1.0
From: "Admin" <no-reply@domain.tld>
Content-Type: multipart/alternative; boundary="_av-aRPROueRxMYL7a0Ro00ndA"
Message-Id: <20150302.ABCDEFGHIJKLMNOPQRSTUVWYXZ@domain.tld>
Date: Mon,  2 Mar 2015 15:41:38 +0100 (CET)

--_av-aRPROueRxMYL7a0Ro00ndA
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Plain text

--_av-aRPROueRxMYL7a0Ro00ndA
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Manuel Arwed Schmidt
  • 3,376
  • 2
  • 18
  • 28

3 Answers3

1

Short answer:

Use the <pre> tag to enforce line breaks to be shown as line breaks in HTML.

Longer Answer:

What you are proposing (using wordwrap function) splits the sending message body into manageable chunks, but won't affect the way it is displayed in a client. In HTML, white space including "line breaks", tabs and multiple spaces are converted into a single space. This means that your "soft line breaks" have no meaning when viewing your HTML in the web browser. The output will continuously flow until it receives a <br> or other tag to break the content.

If you have some mail sending restrictions that enforce the 79 character limit, then what you have done may suffice, even if it shows in more that 79 characters in the client. (Usually the "79 characters" limit is for the plain text portion of an email, not the HTML portion - so check where that restriction comes from.)

If you must have it displayed in 79 characters, then using the <pre> tag in HTML will tell it not to collapse white space, thus your breaks should show where you ask them to be.

Robbie
  • 17,605
  • 4
  • 35
  • 72
-1

Set the 'Content-Transfer-Encoding' to '7bit' instead of 'Quoted-Printable.' Some mail clients, such as Outlook and Thunderbird, appear to insert double spacing line breaks at every line. The reason is that the 'content-transfer-encoding' in MIME is set to 'quoted-printable' which adds Carriage Return Line Feed (CRLF) line breaks to the source content of the email which are characters interpreted by these mail clients.

Francisco Félix
  • 2,413
  • 13
  • 16
-2

%20 is a soft line break. You don't need to do anything special. If you type a long line like this, Gmail will break the line for you:

echo '<a href="mailto:me@example.com?subject=Reply%20to%20this%20Post& body=Enter%20your%20reply%20below%20this%20line%20and%20hit%20SEND.%20You%20may%20also%20attach%20an%20image%20to%20your%20reply!%20Repeat:%20Enter%20your%20reply%20below%20this%20line%20and%20hit%20SEND.%20You%20may%20also%20attach%20an%20image%20to%20your%20reply!">click me</a>';

malcanso
  • 100
  • 2
  • 7
  • For me, %20 represents an url encoded SPACE character. It got nothing to do with a soft line break as mentioned in the RFC (point 5): http://www.faqs.org/rfcs/rfc2045.html – Manuel Arwed Schmidt Mar 09 '15 at 16:28
  • You're correct about the RFC. I think the answer is this: a mailto link in a web page is not an email. The RFC does not apply to it. The user's mail client is responsible for inserting soft breaks. Gmail, for example, handles that. So the html author doesn't need to think about this at all. My solution works, but not for the reason I said before. – malcanso Mar 09 '15 at 20:49