Skip to content
Snippets Groups Projects
Unverified Commit a51ee59d authored by Paul B's avatar Paul B
Browse files

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
parent 7b0e42be
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment