?
avatar OctavianC
OctavianC
24 Mar 2021

Steps to reproduce the issue

  • System - Global Configuration - Server - Mail Settings - Mailer should be set to "PHP Mail"
  • Run the following code. Make sure images/joomla_black.png exists so that the attachment works (it's there by default)
$mailer = JFactory::getMailer();
$app = JFactory::getApplication();
$email = 'your@email'; // replace this with an actual email
$mailer->sendMail($app->get('mailfrom'), $app->get('fromname'), $email, 'Test mail', '<p>This is a <strong>test</strong> message</p>', true, null, null, JPATH_SITE . '/images/joomla_black.png');

Expected result

An HTML formatted email with an image attachment.

This is a test message
[image]

Actual result

The result is that the email is missing the "Content-Type", the "From" you've specified, "MIME-Version", "Date" and thus treated as plain text. The boundaries and the encoded file contents show up in the message.

This is a multi-part message in MIME format.

--b1_43f3380ed0c3e4478bc52721618a6943
Content-Type: text/html; charset=us-ascii

<p>This is a <strong>test</strong> message</p>


--b1_43f3380ed0c3e4478bc52721618a6943
Content-Type: application/octet-stream; name="joomla_black.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=joomla_black.png
[base64 encoded text]

System information (as much as possible)

PHP 8
Joomla! 3.9.25

Additional comments

I've been scratching my head the past day to find out what causes this and noticed this in PHP mail():

Multiple extra headers should be separated with a CRLF (\r\n)

It seems PHPMailer separates them by \n and PHP treates them as a single header. Most likely the PHPMailer class needs to be updated so that Joomla! 3 can support PHP 8. To fix this I've modified the mailPassthru() function in libraries/vendor/phpmailer/class.phpmailer.php:

$header = str_replace("\n", "\r\n", $header);

Basically the $additional_headers param from mail() will now receive the headers separated as CRLF and the email will be sent correctly. Not sure if this has any other implications though.

avatar OctavianC OctavianC - open - 24 Mar 2021
avatar joomla-cms-bot joomla-cms-bot - change - 24 Mar 2021
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 24 Mar 2021
avatar zero-24
zero-24 - comment - 24 Mar 2021

We have to do changes to phpmailer ourself anyway as our version is out of support so can you please send a PR against: https://github.com/joomla-backports/PHPMailer

After thats merged there we have to do a release and update composer to point to our fork.

avatar zero-24
zero-24 - comment - 24 Mar 2021

Ah and did you checked whether that works on php lower than 8 too? Or do we need to do a version conditional?

avatar OctavianC
OctavianC - comment - 24 Mar 2021

Just tested this with PHP 7.3.27 and both \n and \r\n seem to work correctly.

avatar PhilETaylor
PhilETaylor - comment - 25 Mar 2021
avatar alikon
alikon - comment - 26 Mar 2021

can someone remember me why we need a backport and not using the last one ?
https://github.com/PHPMailer/PHPMailer/releases/tag/v6.3.0

avatar OctavianC
OctavianC - comment - 26 Mar 2021

It's probably because it requires PHP 5.5+ and Joomla! 3 still supports 5.3.10

avatar PhilETaylor
PhilETaylor - comment - 26 Mar 2021

This is exactly the kind of institutional knowledge that should be documented somewhere so that it is not lost and can be referred to.

avatar PhilETaylor
PhilETaylor - comment - 26 Mar 2021

This was fixed upstream in PHPMailer/PHPMailer#2188 and I have made a PR to merge that same change in the Joomla force of phpMailer here: joomla-backports/PHPMailer#1 please test that and report back on that PR. This can then get closed.

avatar PhilETaylor
PhilETaylor - comment - 27 Mar 2021

Sorry to be the downer here, but I cannot replicate this with PHP 8.0.3 and Joomla 3 staging

I used your example code to send an email, caught it with MailHog, and downloaded to Mac Mail. And I can find no issue, the image renders correctly in both MailHog and Mac Mail

Screenshot 2021-03-27 at 17 32 05

avatar PhilETaylor
PhilETaylor - comment - 27 Mar 2021

@OctavianC Please test joomla-backports/PHPMailer#3

I could never replicate the problem, but I dont seem to make it worse with the proposed fix, which is similar to the upstream fix, but because of the change in major version is harder to apply I applied a light touch

avatar zero-24 zero-24 - change - 30 Mar 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-03-30 21:15:37
Closed_By zero-24
avatar zero-24 zero-24 - close - 30 Mar 2021
avatar PhilETaylor
PhilETaylor - comment - 31 Mar 2021

Although this is closed, and the fix merged to the back port, the back port still needs to be implemented in the cms right? @zero-24

avatar zero-24
zero-24 - comment - 31 Mar 2021

Yes thats right the plan is to point our conposer.json to the backports repo and than pull the changes from there into the CMS.

avatar PhilETaylor
PhilETaylor - comment - 31 Mar 2021

my point, badly made, was that maybe its worth keeping this issue open until those additional tasks have taken place so it doest get lost.

avatar PhilETaylor
PhilETaylor - comment - 31 Mar 2021

Also as far as I know my changes were not tested by anyone other than myself... do you really trust me that well.. I dont :)

avatar zero-24 zero-24 - change - 31 Mar 2021
Status Closed New
Closed_Date 2021-03-30 21:15:37
Closed_By zero-24
avatar zero-24 zero-24 - reopen - 31 Mar 2021
avatar zero-24
zero-24 - comment - 31 Mar 2021

Ah reopend that Issue here until the PR against the CMS is made.

avatar OctavianC
OctavianC - comment - 31 Mar 2021

Well, there's one more person who tested this - I just got caught up with other issues and totally forgot to reply.

Before patch:
before-php8

After patch:
after-php8

Server information, if it makes any difference:

php: Linux server6.ghesi.net 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
dbserver: mysql
dbversion: 5.5.5-10.2.37-MariaDB-cll-lve
dbcollation: utf8mb4_general_ci
dbconnectioncollation: utf8mb4_general_ci
phpversion: 8.0.3
server: LiteSpeed
sapi_name: litespeed
version: Joomla! 3.9.25 Stable [ Amani ] 02-March-2021 15:00 GMT
platform: Joomla Platform 13.1.0 Stable [ Curiosity ] 24-Apr-2013 00:00 GMT
useragent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0

I can easily reproduce this bug with any shared hosting (linux + PHP8) that can send mail using PHP's mail() function. Not sure about your Mac setup since I'm not familiar with that @PhilETaylor

avatar PhilETaylor
PhilETaylor - comment - 31 Mar 2021

Pleased it worked for you. I thought it would work as its the same fix that the PHPMailer project used.

The credit for the actual fix was with the upstream project, as they had already done the hard work, all I did was find a way to merge it into the older version of PHPMailer.

avatar PhilETaylor
PhilETaylor - comment - 31 Mar 2021

Ah reopend that Issue here until the PR against the CMS is made.

@zero-24 Is a release going to be made here? https://github.com/joomla-backports/PHPMailer/releases or are we going to target a commit (bad) in the composer.json?

avatar zero-24
zero-24 - comment - 31 Mar 2021

IIRC with lessphp we used the branch but i agree that doing an propper release would be better. We would have to take a look what makes the most sense in our case.

avatar PhilETaylor
PhilETaylor - comment - 27 Apr 2021

Please, Who do I need to ask "nicely" to make a release so that we can make progress on this please?

avatar Quy
Quy - comment - 19 May 2021

OK to close per #33772?

avatar PhilETaylor
PhilETaylor - comment - 19 May 2021

Yup

avatar alikon alikon - change - 19 May 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-05-19 06:22:44
Closed_By alikon
avatar alikon alikon - close - 19 May 2021
avatar alikon
alikon - comment - 19 May 2021

closing as per #33772

Add a Comment

Login with GitHub to post a comment