Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP8/exim4/linux issue with email headers (Dokuwiki 2022-07-31 Igor) #3733

Open
AnonTester opened this issue Aug 5, 2022 · 3 comments
Open

Comments

@AnonTester
Copy link

I've upgraded PHP from 7.4 to 8.1 on a Debian 10 linux system with exim4 4.94.2 running Dokuwiki 2022-07-31 Igor and email notifications were not sent out. It was working perfectly fine before the php upgrade.
The email system complained that "A message that you sent contained no recipient addresses, and therefore no delivery could be attempted." even though bcc was used.
The email header looked like this:

To: 
Subject: [WIKINAME] page added: linux:php8-debian11
Return-Path: [removed](mailto:removed)
 X-Mailer: DokuWiki
 X-Dokuwiki-User: removed
 X-Dokuwiki-Title: Wiki Title
 X-Dokuwiki-Server: removed
 X-Auto-Response-Suppress: OOF
 List-Id: removed
 Date: Fri, 05 Aug 2022 10:53:45 +0100
 Bcc: [removed](mailto:removed)
 List-Unsubscribe:
   [<http:https://removed/doku.php?id=linux:php8-debian11&do=subscribe>](http:https://removed/doku.php?id=linux:php8-debian11&do=subscribe)
 Message-Id: [<linuxphp8-debian11rev1659693211@removed>](mailto:linuxphp8-debian11rev1659693211@removed)
 MIME-Version: 1.0
 Content-Type: multipart/alternative;
   boundary="__________de3b83efafa486f9769e8bba4e8fa55eXX"
Message-Id: <E1oJu1Z-007rDY-Mw@removed>
From: www-data <www-data@removed>
From: www-data [<removed>](mailto:removed)
X-rewrote-sender: www-data@removed
Date: Fri, 05 Aug 2022 10:53:45 +0100

Note the spaces in front of several headers including the BCC line.

After investigating it turned out that this is related to the change of the mail() function in php8 to always use CRLF ("\r\n") in email headers instead of just the typical LF ("\n") on linux systems.
I found the solution in inc/mail.php by changing the MAILHEADER_EOL definition at the top from "\n" to "\r\n".
This might not be the best place to change this and I do not know how other mail systems or operating systems would react to this, but might be useful to someone and preparing a proper adjustment probably based on PHP version and operating system.

if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\r\n");
@Klap-in
Copy link
Collaborator

Klap-in commented Aug 5, 2022

Side note: Defining such constants (upgrade-prove) is possible via the inc/preload.php file. This file is loaded before the wiki is initiated. (https://www.dokuwiki.org/devel:preload)

This change is not mentioned in the page about the mail() function (https://www.php.net/manual/en/function.mail.php). How did you find it? by testing? or is it also documented?
According the remark in the code, CRLF seems to be according the standard?

https://github.com/splitbrain/dokuwiki/blob/8e8e73a7317718894743233168d39f27aec8ded2/inc/Mailer.class.php#L14-L18

@AnonTester
Copy link
Author

Thanks. I found this bug report on drupal's issue tracker which links some issues on the php tracker. Found one more specific pho one but can't find it again right now, will post it later.

https://www.drupal.org/project/drupal/issues/3270647

@AnonTester
Copy link
Author

This is the bug report on php stating that php's mail function was changed and now uses CRLF instead of LF. The effect of the issue described in it is different from the resulting mail in dokuwiki, bit it helped figuring out where to look for a workaround:
php/php-src#8086

Took me various testing and googling but I understand what's happening now and how the email ended up the way it does. I couldn't figure out where the single space indentations were coming from until I found the explanation in the exim documentation https://www.exim.org/exim-html-current/doc/html/spec_html/ch-message_processing.html section 2.

This results in the following explanation:
The first line in the header is To which comes directly from php's mail function. This uses CRLF line ending now. Exim treats this as the format of the mail and uses CR as the line endings and ignores the LF if preceded by CR. Any bare LF is treated as a newline for the respective individual header and preceded by a space to avoid ending the header line.
Therefore, from Exim's perspective, all the additional headers from dokuwiki are actually just the single header Return-Path which spans over multiple lines. The Bcc and the others are not separate headers and aren't seen by Exim and with the empty To, there is nowhere for exim to actually send the mail to and it complaints.

Changing the define statement from the bare LF to CRLF makes the line endings consistent throughout the email passed to exim and all is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants