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

Use Q-encode to wrap too long headers #1840

Merged
merged 5 commits into from
Sep 25, 2019
Merged

Use Q-encode to wrap too long headers #1840

merged 5 commits into from
Sep 25, 2019

Conversation

caugner
Copy link
Contributor

@caugner caugner commented Sep 23, 2019

Fixes #1469.
Fixes #1525.

@caugner

This comment has been minimized.

@Synchro
Copy link
Member

Synchro commented Sep 23, 2019

The test case should be updated for the new format, however I'd say that it's not quite correct anyway - that example should be using us-ascii for that header value; While it's workable, UTF-8 is excessive for something that doesn't contain 8-bit chars, and email should try and use minimal encodings (e.g. see the 7bit fallback used for message bodies).

@caugner
Copy link
Contributor Author

caugner commented Sep 23, 2019

As for the test case, this is fixed in 8701f3b 6ee4d8a f547145.

Regarding the charset selection, isn't this a separate issue of PHPMailer::encodeHeader? Currently it always Q-encodes any value using the PHPMailer::$CharSet, whereas it could be more clever and prefer less expensive charsets such as us-ascii or 7bit if applicable.

@Synchro
Copy link
Member

Synchro commented Sep 23, 2019

Yes, exactly that (though 7bit is a CTE, not a charset). A call to has8bitChars() will tell you whether it can be downgraded safely.

src/PHPMailer.php Outdated Show resolved Hide resolved
@Synchro
Copy link
Member

Synchro commented Sep 23, 2019

I wonder if this will fix #1525 as well?!

@caugner
Copy link
Contributor Author

caugner commented Sep 23, 2019

UTF-8 is excessive for something that doesn't contain 8-bit chars

Can you illustrate why UTF-8 is more excessive in this case:

=?UTF-8?Q?eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee?=
  =?UTF-8?Q?eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee?=

vs.

=?us-ascii?Q?eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee?=
  =?us-ascii?Q?eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee?=

Isn't us-ascii a subset of UTF-8?

@Synchro
Copy link
Member

Synchro commented Sep 23, 2019

While the content is indeed identical and compatible, marking it as UTF-8 requires firing up a complete unicode engine to decode it on the receiver, which is unnecessary overhead if we know in advance it's plain ASCII. In PHP for example, we would not be able to decode it easily if we didn't have the mbstring extension enabled, whereas it would present no problems in us-ascii.

It's like putting literal IPs first in SPF records; it's not a requirement, but it makes life easier for receivers.

src/PHPMailer.php Outdated Show resolved Hide resolved
Previously, headers exceeding the maximum line length without
any special characters were only folded. This lead to problems
with long filenames (#1469) and long headers in general (#1525).

Now, long headers are always Q-encoded (and still folded).
Previously, headers were Q-encoded using the message
charset, e.g. UTF-8. This is excessive for ASCII
values, as it requires a unicode engine.

Now, we use ASCII if we only find 7-bit characters.
@caugner
Copy link
Contributor Author

caugner commented Sep 23, 2019

I wonder if this will fix #1525 as well?!

I think so.

@Synchro Maybe you could have a second third look? I have now refactored my PR as follows:

  • Aligned commit messages with the usual style in this repository (consistency).
  • Separated encoding selection from encoding (readability, separation of concerns).
  • Applied charset selection to B-encoding as well (consistency).

src/PHPMailer.php Outdated Show resolved Hide resolved
Previously, we calculated the maximum
line length for header encoding both
for B- and Q-encoding, even though
they share the same limits.

Now, we calculate these once for both.
@Synchro Synchro merged commit 21b35dc into PHPMailer:master Sep 25, 2019
@Synchro
Copy link
Member

Synchro commented Sep 25, 2019

This was a great bit of work, solving several long-standing bugs that I'd been procrastinating about! Thank you very much for all your effort. This set of changes and other things that have happened lately mean that I will release this as PHPMailer 6.1 soon. Would you be interested in joining the PHPMailer org?

@caugner
Copy link
Contributor Author

caugner commented Sep 25, 2019

@Synchro Thank you for your reviews and the merge. And sure, feel free to add me to the org. I might like to tackle the header name vs. header value line length limitation at some point.

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