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 daniel - June 14, 2022

Thanks for the info Deborah! Keep me posted on what you find with other email clients.

A few other questions that might help narrow things down:

  • If possible, are you able to test using a Pre-v3.55 CMSB install running PHP 8? This is outside compatibility and will likely generate errors so I understand if you're not able to do this, but it would help to know if the combination of CMSB and PHP versions has any effect, of if it's purely an issue with PHP 8.
  • For the site(s) having the issue, on the Email Settings configuration page, is "How to send mail" set to use PHP's built-in mail() function, or is it set up to send through an SMTP server? 

Thanks!

Daniel
Technical Lead
interactivetools.com

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