Browse Source

Do not send empty mail

Guénaël Muller 7 years ago
parent
commit
eae3eff43b

+ 17 - 4
tracim/tracim/lib/email_fetcher.py View File

331
         #  if no from address for example) and catch it here
331
         #  if no from address for example) and catch it here
332
         while mails:
332
         while mails:
333
             mail = mails.pop()
333
             mail = mails.pop()
334
+            body =  mail.get_body(
335
+                           use_html_parsing=self.use_html_parsing,
336
+                           use_txt_parsing=self.use_txt_parsing)
337
+            from_address = mail.get_from_address()
338
+
339
+            # don't create element for 'empty' mail
340
+            if not body:
341
+                logger.warning(
342
+                    self,
343
+                    'Mail from {} has not valable content'.format(
344
+                        from_address
345
+                    ),
346
+                )
347
+                continue
348
+
334
             msg = {'token': self.token,
349
             msg = {'token': self.token,
335
-                   'user_mail': mail.get_from_address(),
350
+                   'user_mail': from_address,
336
                    'content_id': mail.get_key(),
351
                    'content_id': mail.get_key(),
337
                    'payload': {
352
                    'payload': {
338
-                       'content': mail.get_body(
339
-                           use_html_parsing=self.use_html_parsing,
340
-                           use_txt_parsing=self.use_txt_parsing),
353
+                       'content': body,
341
                    }}
354
                    }}
342
             try:
355
             try:
343
                 logger.debug(
356
                 logger.debug(

+ 13 - 2
tracim/tracim/lib/email_processing/sanitizer.py View File

1
+import typing
1
 from bs4 import BeautifulSoup, Tag
2
 from bs4 import BeautifulSoup, Tag
2
 from tracim.lib.email_processing.sanitizer_config.attrs_whitelist import \
3
 from tracim.lib.email_processing.sanitizer_config.attrs_whitelist import \
3
     ATTRS_WHITELIST
4
     ATTRS_WHITELIST
31
     """
32
     """
32
 
33
 
33
     @classmethod
34
     @classmethod
34
-    def sanitize(cls, html_body: str) -> str:
35
+    def sanitize(cls, html_body: str) -> typing.Optional[str]:
35
         soup = BeautifulSoup(html_body, 'html.parser')
36
         soup = BeautifulSoup(html_body, 'html.parser')
36
         for tag in soup.findAll():
37
         for tag in soup.findAll():
37
             if cls._tag_to_extract(tag):
38
             if cls._tag_to_extract(tag):
43
                         del tag.attrs[attr]
44
                         del tag.attrs[attr]
44
             else:
45
             else:
45
                 tag.unwrap()
46
                 tag.unwrap()
46
-        return str(soup)
47
+
48
+        if cls._is_content_empty(soup):
49
+            return None
50
+        else:
51
+            return str(soup)
52
+
53
+    @classmethod
54
+    def _is_content_empty(cls, soup):
55
+        img = soup.find('img')
56
+        txt = soup.get_text().replace('\n', '').strip()
57
+        return (not img and not txt)
47
 
58
 
48
     @classmethod
59
     @classmethod
49
     def _tag_to_extract(cls, tag: Tag) -> bool:
60
     def _tag_to_extract(cls, tag: Tag) -> bool: