kodus / mail

Simple, modern library of services for sending UTF-8 e-mail
38 stars 5 forks source link

quoted-printable and dot-stuffing filters applied in wrong order! #21

Open rhl-jfm opened 12 hours ago

rhl-jfm commented 12 hours ago

TL;DR kodus/mail sometimes fails to do dot-stuffing on leading "." when sending mail via SMTP, leading to an SMTP protocol error. A possible fix is to use stream_filter_prepend() instead of stream_filter_append() in Kodus\Mail\Writer::writeFiltered()

In the current implementation, quoted-printable encoding and SMTP dot-stuffing are both implemented using PHP stream filters. When calling Kodus\Mail\SMTP\SMTPMailService::send(), the smtp.dot_stuffing is appended to the output socket before convert.quoted-printable-encode. This is a mistake, as the SMTP dot-stuffing is a lower level concern (a part of the protocol) and must be performed as the last step.

Usually this works out fine, but sometimes convert.quoted-printable-encode will break up a long line, putting a single dot on a line by itself. Lines with single dots are exactly what the smtp.dot_stuffing filter is meant to handle, but it never sees this output.

An example of a mail body that does not work with the current kodus/mail is:

Test mail 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890.
More text.

After quoted printable (as configured by kodus/mail) it becomes:

Test mail 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890=
.
More text.

I see two possible fixes:

  1. Replace the use of stream_filter_append() by stream_filter_prepend() in Kodus\Mail\Writer::writeFiltered(). This causes the quoted-printable filter to be applied before dot-stuffing.
  2. Refactor the code to not use (two) stream filters or to be more explicit about the ordering.