From a51ee59d1c5ea242ecd4c55880fd164c3cca297b Mon Sep 17 00:00:00 2001
From: Paul B <paul@bonaud.fr>
Date: Mon, 15 Apr 2019 18:24:10 +0200
Subject: [PATCH] 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
---
 app/classes/Framadate/Services/MailService.php | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/app/classes/Framadate/Services/MailService.php b/app/classes/Framadate/Services/MailService.php
index 73a8c085..5fcccfaf 100644
--- a/app/classes/Framadate/Services/MailService.php
+++ b/app/classes/Framadate/Services/MailService.php
@@ -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);
+    }
 }
-- 
GitLab