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 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 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