Email message body not formatted correctly w/PHP 8

9 posts by 3 authors in: Forums > CMS Builder
Last Post: August 30, 2022   (RSS)

By Deborah - June 7, 2022

I am experiencing issues with CMSB 3.55 and 3.56 emails.

It's happening with various email templates, the Auto Backup plugin email, and the password reset email. The body of the messages contain raw HTML and the Auto Backup does not send a zip attachment of the backup. I am using PHP's built-in mail() function in the settings.

The following two examples happen with CMSB v3.55 or v3.56 with PHP 8.0.18 or 8.1.5 (same results with any combo). Setting back to PHP 7.4 works fine.

#1 - Example from /admin.php?menu=forgotPassword

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.=
w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=3D"http://www.=
w3.org/1999/xhtml">
<head>
<meta http-equiv=3D"Content-Type" content=3D=
"text/html; charset=3Dutf-8">
<title>CMS Password Reset</title>
</head>=

<body>

<style type=3D"text/css">
  p { margin-bottom: 1em; }
</=
style>

<p>Hi you@domain.net,</p>
<p>You requested a passw=
ord reset for CMS.</p>
<p>To reset your password click this link:<br /><a=
 href=3D"https://www.domain.net/cms/admin.php?menu=3DresetPasswo=
rd&userNum=3D1&resetCode=3D0226ab5846b92c5ed0da66b924504297f87abdce">https:=
//www.domain.net/cms/admin.php?menu=3DresetPassword&userNum=3D1&=
resetCode=3D0226ab5846b92c5ed0da66b924504297f87abdce</a></p>
<p>This requ=
est was made from IP address: (removed)</p></body>
</html>

#2 - Partial Example from Auto Backup v1.04 Plugin:

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

Automatically generated backup file.

--_=_swift_v4_1654624802_b5c4dc98cc1f9bcb6a421e7fe5f9af33_=_
Content-Type: application/octet-stream;
 name=myweb-v3.56-hourly-14.sql.php.gz
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=myweb-v3.56-hourly-14.sql.php.gz

H4sIAAAAAAACA+1d63bbyJH+76foYydjKytSAHinbM/IsjxWRpZ1JHqcnChHbgJNsscAGtMAJHP2
5Jx9jf2Xv9nHyLzJPslWNS4ESZAUKNEryXA4Cgl0V3VVfV1d1RegUiHPv/dGHrE4e/a0N+I+gY8r
AkKJJ8VQUocMuM2qT7d2CfvCg13y/ctHjx5VKvAhPdq3GfEDGZpBKBkZCEkCde3Tk97eq6ODi5PT
... (removed large block)

--_=_swift_v4_1654624802_b5c4dc98cc1f9bcb6a421e7fe5f9af33_=_--

I'm not sure what to do or check. Thanks in advance for any ideas.

~ Deborah

By daniel - June 14, 2022

Hi Deborah,

This is an odd issue, but not the first we've seen it. Could you let me know what email client you're using, and possibly try to send out to some different email addresses using different clients (e.g. gmail, outlook, etc.)? I wasn't able to find any specific discrepancies between how PHP 7.4 and 8.1 send out emails, so it may help to check for any client differences.

Thanks,

Daniel
Technical Lead
interactivetools.com

By Deborah - June 14, 2022

Daniel, I appreciate your reply. If you can't replicate the issue, then there is hope!

I'm viewing email received in Microsoft Outlook 2016. Emails generated by CMSB installs prior to 3.55 or 3.56 (with older PHP versions) still look good.

I'll try sending to other email clients and let you know what I find.

Thanks again!
~ Deborah

By Deborah - June 15, 2022

Daniel, thanks for the ideas for narrowing things down. No luck yet, but here's where I'm at:

  • I tried, but am not able to run pre-v3.55 installs when switched to PHP 8, because errors prevent the admin login screen from displaying.
    ERROR: "Too few arguments to function _errorlog_catchRuntimeErrors()...." (removed the rest of this message)
  • I am using PHP's built-in mail() function. I've not been able to get SMTP connections to connect for many versions and am not able to with 3.56 either.
  • Today I tried a fresh install of 3.56 on a different server (updated to PHP 8.0.20) and got the same email template errors, with the HTML displaying in the body of the message.
  • In addition to sending password reset emails to Microsoft Outlook on my PC, I also sent to Gmail and Outlook.com and see the same email issue.

I've now tested on at least 4-5 different installs over two different servers and all emails are coming through similarly.
There are equal signs "=" randomly inserted into the messages, which could be breaking the HTML
PARTIAL EXAMPLE:

<p>Hi you@yourdomain.com,</p>
<p>You request=
ed a password reset for CMS Builder.</p>
<p>To reset your password click =
this link:
...

I've exhausted all of my testing ideas here. If you have other suggestions of things for me to try, I'd be happy to do so.

Thanks for your help on this!
~ Deborah

By Dave - June 16, 2022

Hi Deborah, 

We had a look into this and it appears to be a known issue with PHP 8:

They changed how the internal PHP mail() function works so that it requires a multicharacter line ending (\r\n instead of \r\n or \n).  We can try changing it, but it may cause it not to work on other servers.  The PHP docs reference that here: https://www.php.net/manual/en/function.mail.php

Note: If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.

Sometimes what we do in these cases is add code that detects the PHP version to work around version-specific issues.  Often they revert it back a few versions later if it breaks too many servers.

We'll need to investigate further to find a universally workable solution, but try this: 

  • Open cmsb/3rdParty/SwiftMailer5/classes/Swift/Transport/SendmailTransport.php
  • Search for "\n"
  • Comment out the line in the else block
if (false === strpos($command, ' -i') && false === strpos($command, ' -oi')) {
  $buffer->setWriteTranslations(array("\r\n" => "\n", "\n." => "\n.."));
} else {
  # Comment this line # $buffer->setWriteTranslations(array("\r\n" => "\n"));
}


Let me know if that makes a difference.  If not could you fill out a second level support request here: https://www.interactivetools.com/support/request/

Hope that helps!

Dave Edis - Senior Developer

interactivetools.com

By Deborah - June 16, 2022

Dave,

Your idea seems a good explanation for what I'm seeing with the random message line breaks.

I tried commenting out the else block, but still see the same. I'll submit a support request, as you suggested.

Thanks very much for looking at this.

~ Deborah

By daniel - August 29, 2022

Hi all,

We were finally able to resolve this issue. It can be fixed by commenting out these lines (165-168) in /cmsb/3rdParty/SwiftMailer5/classes/Swift/Transport/MailTransport.php:

$headers = str_replace("\r\n", PHP_EOL, $headers);
$subject = str_replace("\r\n", PHP_EOL, $subject);
$body = str_replace("\r\n", PHP_EOL, $body);
$to = str_replace("\r\n", PHP_EOL, $to);


Note that this is a system-specific fix, i.e. only make this fix if your site is exhibiting the issue. It can happen on some Linux servers running PHP 8+, but not all. It is not backwards-compatible with PHP 7, so it won't be added to the official CMSB release. We will instead continue looking at a long-term fix that can be backwards compatible.

Cheers,

Daniel
Technical Lead
interactivetools.com

By Deborah - August 30, 2022

Daniel,

The modified MailTransport.php solved the mail formatting issue for me. I've tested two different CMSB installs on two different servers, both with PHP 8.1.

Thank you so much!
~ Deborah