Make the system mails use "Multi-part MIME"

linkchilla

Network license
License owner
Joined
Aug 31, 2017
Messages
60
Likes
13
Points
58
Location
Northern Germany
#1
Please can you make all system generated mails provide a plain-text alternative in their mail body?

I think it is an important "best practices", because:
  • Spam filters like to see a plain text alternative. HTML-only emails are a red flag for spam filters.
  • Some email-clients and apps won't or can't handle HTML. In cases like this, the plain text email will display.
  • Some people (like me) simply prefer it. Plain and simple.
 

Rodolfo

Chevereto Guru-Guru
Staff member
Joined
Oct 7, 2008
Messages
16,134
Likes
4,177
Points
237
Location
Chevereto HQ
Website
rodolfoberrios.com
#2
It is supposed that PHPMailer does that auto, but since the messages are plain, it not generates multipart but just a weird text with some HTML tags.
 

linkchilla

Network license
License owner
Joined
Aug 31, 2017
Messages
60
Likes
13
Points
58
Location
Northern Germany
#3
Mail send from "Dashboard -> Tools" is send correctly as plain text and "Content-Type: text/plain; charset=UTF-8". I guess then, somewhere html-code gets injected and the other mails are detected as HTML by PHPMailer.
 

Rodolfo

Chevereto Guru-Guru
Staff member
Joined
Oct 7, 2008
Messages
16,134
Likes
4,177
Points
237
Location
Chevereto HQ
Website
rodolfoberrios.com
#4
But phpmailer adds the multipart when it detects html. It doesn't send html without also sending plain text
 

linkchilla

Network license
License owner
Joined
Aug 31, 2017
Messages
60
Likes
13
Points
58
Location
Northern Germany
#5
How about after #126 in "/app/lib/functions.php":
PHP:
if($body != strip_tags($body)) {
   $mail->IsHTML(true);
   $mail->AltBody = strip_tags($body);
}
I did a quick test and it seems to work as expected.
 

linkchilla

Network license
License owner
Joined
Aug 31, 2017
Messages
60
Likes
13
Points
58
Location
Northern Germany
#7
"strip_tags" leaves some empty lines behind. Do you know an elegant way to get rid of these unneeded lines?

As a suggestion, maybe it is a good idea to loose the html in "app/themes/Peafowl/mails/*.php"? Anyone who want to use fancy mails with images, banners and stuff whould override these snippets anyway. The function "send_mail()" in "/app/lib/functions.php" makes sure, that it is send correctly as plain-text or, if html is detected, as multipart.
 

linkchilla

Network license
License owner
Joined
Aug 31, 2017
Messages
60
Likes
13
Points
58
Location
Northern Germany
#9
It seems that "strip_tags" removes the html-tag, but not the linefeed and/or carrier-return. So all lines with a removed tag become empty lines. For instance the body of the plain-text mail starts with two empty lines. Doesn't do any harm, but looks ugly, though.
 

linkchilla

Network license
License owner
Joined
Aug 31, 2017
Messages
60
Likes
13
Points
58
Location
Northern Germany
#11
Of course!

Now I have:

PHP:
if($body != strip_tags($body)) {
    $mail->IsHTML(true);
    $plaintext = trim((strip_tags($body))," \t\n\r\0\x0B");          
    $mail->AltBody = $plaintext;
}
It also leaves the original html part intact and adds the plaintext message. Still no beauty, but works solid.

Code:
This is a multi-part message in MIME format.

--b1_0f93f6d39a0fff7f0e7830c556e585df
Content-Type: text/plain; charset=us-ascii

Hi Testing, welcome to Linkchilla
Now that your account is ready you can enjoy uploading your images, creating albums and setting the privacy of your content as well as many more cool things that you will discover.
By the way, here is you very own awesome profile page: testing. Go ahead and customize it, its yours!.
Thank you for joining,

Linkchilla
--
This email was sent from Linkchilla https://www.linkchilla.com


--b1_0f93f6d39a0fff7f0e7830c556e585df
Content-Disposition: attachment; filename="Anlage.html"
Content-Type: text/html; charset="us-ascii"; name="Anlage.html"

<html>
<body>
Hi Testing, welcome to Linkchilla<br><br>
Now that your account is ready you can enjoy uploading your images, creating albums and setting the privacy of your content as well as many more cool things that you will discover.<br><br>
By the way, here is you very own awesome profile page: <a href="https://www.linkchilla.com/testing">testing</a>. Go ahead and customize it, its yours!.<br><br>
Thank you for joining,
<br>
Linkchilla
<br><br>--<br>
This email was sent from Linkchilla https://www.linkchilla.com</body>
</html>



--b1_0f93f6d39a0fff7f0e7830c556e585df--
BTW: The correct way to use a signature divider is (without the quotes)
Code:
"-- "
Note the trailing space.
 

Rodolfo

Chevereto Guru-Guru
Staff member
Joined
Oct 7, 2008
Messages
16,134
Likes
4,177
Points
237
Location
Chevereto HQ
Website
rodolfoberrios.com
#12
You should use trim() alone, without character mask. By default, it handles " \t\n\r\0\x0B" so you don't need to add it on your own.
 

linkchilla

Network license
License owner
Joined
Aug 31, 2017
Messages
60
Likes
13
Points
58
Location
Northern Germany
#14
Just updated to 3.10.4 and noticed you changed these lines:

PHP:
$alt_body = trim(strip_tags($body));
if($body != $alt_body) {
    $mail->IsHTML(true);
    $mail->AltBody = $alt_body;
}
I think, this will always send html+plaintext, even when the mail is only plain-text. (e.g.: manually removing all html tags from the mail snippets.)

Because of the early trim, the next if statement will be always "true":
PHP:
if($body != $alt_body) {
I don't think it is intended?
 

Rodolfo

Chevereto Guru-Guru
Staff member
Joined
Oct 7, 2008
Messages
16,134
Likes
4,177
Points
237
Location
Chevereto HQ
Website
rodolfoberrios.com
#15
Trim only deals with chars at the beginning or at the end of the string, the only false positive will be if the email has extra chars in those positions (which is not likely to happen).

Trim won't handle anything inside the string, you need something like this:
PHP:
preg_replace("/[\r\n]+/", "\n\n", trim($strip))
That will deal with all the extra line breaks, but we are just running in circles here. PHPMailer includes methods to do this.

Re-download and you will see it.