Selaa lähdekoodia

Adding management of mail sending error in debug trace and frontend flash message while creating user

guillaume chanaud 7 vuotta sitten
vanhempi
commit
a31597127e
2 muutettua tiedostoa jossa 16 lisäystä ja 4 poistoa
  1. 10 2
      tracim/tracim/controllers/admin/user.py
  2. 6 2
      tracim/tracim/lib/email.py

+ 10 - 2
tracim/tracim/controllers/admin/user.py Näytä tiedosto

@@ -344,12 +344,20 @@ class UserRestController(TIMRestController):
344 344
 
345 345
         api.save(user)
346 346
 
347
+        email_sent = True
347 348
         if send_email:
348 349
             email_manager = get_email_manager()
349
-            email_manager.notify_created_account(user, password=password)
350
+            try:
351
+                email_manager.notify_created_account(user, password=password)
352
+            except Exception:
353
+                email_sent = False
350 354
 
351 355
         api.execute_created_user_actions(user)
352
-        tg.flash(_('User {} created.').format(user.get_display_name()), CST.STATUS_OK)
356
+        if not email_sent:
357
+            tg.flash(_('User {0} created but email was not sent to {1}').format(user.get_display_name(), user.email),
358
+                     CST.STATUS_WARNING)
359
+        else:
360
+            tg.flash(_('User {} created.').format(user.get_display_name()), CST.STATUS_OK)
353 361
         tg.redirect(self.url())
354 362
 
355 363
     @classmethod

+ 6 - 2
tracim/tracim/lib/email.py Näytä tiedosto

@@ -69,6 +69,7 @@ class EmailSender(object):
69 69
             logger.info(self, 'Connecting from SMTP server {}'.format(self._smtp_config.server))
70 70
             self._smtp_connection = smtplib.SMTP(self._smtp_config.server, self._smtp_config.port)
71 71
             self._smtp_connection.ehlo()
72
+
72 73
             if self._smtp_config.login:
73 74
                 try:
74 75
                     starttls_result = self._smtp_connection.starttls()
@@ -82,9 +83,12 @@ class EmailSender(object):
82 83
                     logger.debug(self, 'SMTP login result: {}'.format(login_res))
83 84
                 except Exception as e:
84 85
                     logger.debug(self, 'SMTP login error: {}'.format(e.__str__()))
85
-                
86
+                    raise e
87
+
86 88
             logger.info(self, 'Connection OK')
87 89
 
90
+
91
+
88 92
     def disconnect(self):
89 93
         if self._smtp_connection:
90 94
             logger.info(self, 'Disconnecting from SMTP server {}'.format(self._smtp_config.server))
@@ -96,7 +100,7 @@ class EmailSender(object):
96 100
         if not self._is_active:
97 101
             logger.info(self, 'Not sending email to {} (service desactivated)'.format(message['To']))
98 102
         else:
99
-            self.connect() # Acutally, this connects to SMTP only if required
103
+            self.connect() # Actually, this connects to SMTP only if required
100 104
             logger.info(self, 'Sending email to {}'.format(message['To']))
101 105
             self._smtp_connection.send_message(message)
102 106