mailservice: use a custom html2text callback for plain text emails

The current `html2text` function from the PHPMailer library strips all
html tags and only keeps the inner html.

In the case of <a> tags we need to share the `href` attribute to the
user when he reads his mail in plain text format.

Fixes #419
This commit is contained in:
Paul B 2019-04-15 18:24:10 +02:00
parent 7b0e42bec4
commit a51ee59d1c
No known key found for this signature in database
GPG Key ID: DE331B23748D3A27

View File

@ -69,7 +69,9 @@ class MailService {
$body = $body . ' <br/><br/>' . __('Mail', 'Thank you for your trust.') . ' <br/>' . NOMAPPLICATION . ' <hr/>' . __('Mail', "\"The road is long, but the way is clear…\"<br/>Framasoft lives only by your donations.<br/>Thank you in advance for your support https://soutenir.framasoft.org");
$mail->isHTML(true);
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->msgHTML($body, ROOT_DIR, true);
$mail->msgHTML($body, ROOT_DIR, function ($html) use ($mail) {
return $this->html2text($mail, $html);
});
// Build headers
$mail->addCustomHeader('Auto-Submitted', 'auto-generated');
@ -124,4 +126,16 @@ class MailService {
}
}
}
/**
* Custom "advanced" callback to pass to msgHTML function
*
* @param PHPMailer $mailer a PHPMailer instance
* @param string $html the HTML body of an email
*/
private function html2text(PHPMailer $mailer, $html) {
$html = preg_replace('/<a[^>]*href="([^"]+)"[^>]*>(.*?)<\/a>/si', '${2}: ${1}', $html);
return $mailer->html2text($html);
}
}