Browse Source

Better escaping for mail header with formataddr method

Guénaël Muller 7 years ago
parent
commit
af29ab76ff
2 changed files with 7 additions and 8 deletions
  1. 4 3
      tracim/tracim/lib/email.py
  2. 3 5
      tracim/tracim/lib/notifications.py

+ 4 - 3
tracim/tracim/lib/email.py View File

@@ -4,6 +4,7 @@ import typing
4 4
 from email.message import Message
5 5
 from email.mime.multipart import MIMEMultipart
6 6
 from email.mime.text import MIMEText
7
+from email.utils import formataddr
7 8
 
8 9
 from mako.template import Template
9 10
 from tg.i18n import ugettext as _
@@ -156,11 +157,11 @@ class EmailManager(object):
156 157
             )
157 158
         message = MIMEMultipart('alternative')
158 159
         message['Subject'] = subject
159
-        message['From'] = '{0} <{1}>'.format(
160
+        message['From'] = formataddr((
160 161
             self._global_config.EMAIL_NOTIFICATION_FROM_DEFAULT_LABEL,
161
-            self._global_config.EMAIL_NOTIFICATION_FROM_EMAIL,
162
+            self._global_config.EMAIL_NOTIFICATION_FROM_EMAIL)
162 163
         )
163
-        message['To'] = user.email
164
+        message['To'] = formataddr(('',user.email))
164 165
 
165 166
         text_template_file_path = self._global_config.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_TEXT  # nopep8
166 167
         html_template_file_path = self._global_config.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_HTML  # nopep8

+ 3 - 5
tracim/tracim/lib/notifications.py View File

@@ -5,6 +5,7 @@ import typing
5 5
 from email.header import Header
6 6
 from email.mime.multipart import MIMEMultipart
7 7
 from email.mime.text import MIMEText
8
+from email.utils import formataddr
8 9
 
9 10
 from lxml.html.diff import htmldiff
10 11
 
@@ -207,10 +208,7 @@ class EmailNotifier(object):
207 208
         else:
208 209
             email_address = email_template.replace('{user_id}', '0')
209 210
 
210
-        return '{label} <{email_address}>'.format(
211
-            label = Header(mail_sender_name).encode(),
212
-            email_address = email_address
213
-        )
211
+        return formataddr((mail_sender_name,email_address))
214 212
 
215 213
     @staticmethod
216 214
     def log_notification(
@@ -269,7 +267,7 @@ class EmailNotifier(object):
269 267
 
270 268
         for role in notifiable_roles:
271 269
             logger.info(self, 'Sending email to {}'.format(role.user.email))
272
-            to_addr = '{name} <{email}>'.format(name=role.user.display_name, email=role.user.email)
270
+            to_addr = formataddr((role.user.display_name,role.user.email))
273 271
 
274 272
             #
275 273
             #  INFO - D.A. - 2014-11-06