Adam Baranyai
Adam Baranyai

Reputation: 3867

How to preserve HTML code when sending it with PHPMailer as an EMail

I am trying to send a HTML mail (and a fallback plain text one) with PHPMailer. My problem is, that when I receive the e-mail, the HTML code gets all mengled up, because of the line-breaks that are added to it.

I've tried changing the encoding from quoted-printable, to either 8bit or 7bit, but PHPMailer just simply reverts to quoted-printable every time.

I've managed to find the cause of this in this answer: https://stackoverflow.com/a/33102397/2691879

Basically, it looks like, that adding a line break after every 998 characters, is a must when sending e-mails, but my question is, how can I be assured, that the HTML code is preserved while sending e-mails then?

Am I missing something here?

Unfortunatly, adding the line breaks manually to the template I use, is out of the question, because the intended use of this code, is that the clients of the site would be able to create their own templates to be sent, and also, there are some variable data in the template, that get's replaced before sending.

Here's some code:

self::$Mailer->addAddress($to);
self::$Mailer->Subject=$subject;
self::$Mailer->Body=$template['html'];
self::$Mailer->AltBody=$template['text'];
self::$Mailer->CharSet = 'UTF-8';
self::$Mailer->Encoding = '7bit';
self::$Mailer->send();

The HTML template that I use, is the following:

<h2> Thank you for joining %PORTALNAME <h2><span style='font-size:14px; font-weight: normal;'> You are receiving this e-mail because you (hopefully) created a new member account at %PORTALNAME! In order to be able to fully use your account, you will need to confirm your e-mail address, by clicking on %CONFIRMATIONLINK.<br> If your e-mail client doesn't recognize the above link, you can copy this url <b>%JUSTLINK</b> and paste it in any browser window, to confirm your account. <br><br> If it wasn't you, who opted for this registration, please ignore this message!</span> <br> <h3 style='margin-bottom: 5px;'> Your login credentials: </h3><div style='width: 100px; float: left;'> Username: </div><div style='float: left;'> %EMAIL </div><div style='clear:both;'></div><div style='width: 100px; float: left;'> Password: </div><div style='float: left;'> %PLAINPASSWORD </div><div style='clear:both;'></div>

And the source of the message that I receive looks like this:

Return-Path: <[email protected]>
Delivered-To: [email protected]
Received: from xxx.xxx.xxx
    by xxx.xxx.xxx (Dovecot) with LMTP id wT5IIgnOKVh8fgAAhsLUGA
    for <[email protected]>; Mon, 14 Nov 2016 14:45:29 +0000
Return-path: <[email protected]>
Envelope-to: [email protected]
Delivery-date: Mon, 14 Nov 2016 14:45:29 +0000
Received: from xxx.xxx.xxx ([199.191.58.218]:48854)
    by xxx.xxx.xxx with esmtp (Exim 4.87)
    (envelope-from <[email protected]>)
    id 1c6IVl-0008M6-Bo
    for [email protected]; Mon, 14 Nov 2016 14:45:29 +0000
Received: by xxx.xxx.xxx (Postfix, from userid 48)
    id 7071DAA810F7; Mon, 14 Nov 2016 09:44:49 -0500 (EST)
To: [email protected]
Subject: Account confirmation
X-PHP-Originating-Script: 505:PHPMailer.php
Date: Mon, 14 Nov 2016 16:44:49 +0200
From: PortalName - Account Information <[email protected]>
Message-ID: <[email protected]>
X-Mailer: PHPMailer 5.2.14 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<h2> Thank you for joining PortalName <h2><span style=3D'font-size:14px; font-w=

eight: normal;'> You are receiving this e-mail because you (hopefully) crea=

ted a new member account at PortalName! In order to be able to fully use your a=

ccount, you will need to confirm your e-mail address, by clicking on <a hre=

f=3D'xxx.xxx.xxx/verify-account/xxxxxx/'> this link</a>.<br> I=

f your e-mail client doesn't recognize the above link, you can copy this ur=

l <b>xxx.xxx.xxx/verify-account/xxxxxx/</b> and paste it in an=

y browser window, to confirm your account. <br><br> If it wasn't you, who o=

pted for this registration, please ignore this message!</span> <br> <h3 sty=

le=3D'margin-bottom: 5px;'> Your login credentials: </h3><div style=3D'widt=

h: 100px; float: left;'> Username: </div><div style=3D'float: left;'> xxx.=

[email protected] </div><div style=3D'clear:both;'></div><div style=

=3D'width: 100px; float: left;'> Password: </div><div style=3D'float: left;=

'> xxxxxxxxx </div><div style=3D'clear:both;'></div>

Upvotes: 0

Views: 1512

Answers (2)

Bud Damyanov
Bud Damyanov

Reputation: 31879

You can instruct manually PHPMailer to wrap long lines with build-in function wrapText():

wrapText(string $message, integer $length, boolean $qp_mode = false) : string

Word-wrap message.

For use with mailers that do not automatically perform wrapping and for quoted-printable encoded messages. Parameters

string $message The message to wrap

integer $length The line length to wrap to

boolean $qp_mode Whether to run in Quoted-Printable mode

Also, do not forget to set self::$Mailer->IsHTML(true) immediately after self::$Mailer->Body=$template['html'];

The function IsHTML(true) instructs the class that you are sending HTML email.

Upvotes: 0

Synchro
Synchro

Reputation: 37770

PHPMailer deals with lines that are too long automatically, for example switching to quoted-printable if lines are over 998 chars. However, the PHP mail() function sometimes tries to do the same thing with smaller values, resulting in a broken message with double-encoding. You may have more luck if you send using SMTP instead of mail() (PHPMailer's default) - call $mail->isSMTP(); and set appropriate SMTP options as in the examples provided with PHPMailer, even if it's just localhost.

You're also running an old version of PHPMailer - latest is 5.2.16.

I'd also recommend trying the unreleased PHPMailer 6.0 branch as it makes some major changes to line break handling (you may need to tweak your code a little).

Upvotes: 1

Related Questions