소스 검색

Handle envelop can have several recipients

Son NK 5 년 전
부모
커밋
78ed6eab5a
1개의 변경된 파일20개의 추가작업 그리고 21개의 파일을 삭제
  1. 20 21
      email_handler.py

+ 20 - 21
email_handler.py

@@ -106,26 +106,25 @@ class MailHandler:
         smtp = SMTP(POSTFIX_SERVER, 25)
         msg = Parser(policy=SMTPUTF8).parsestr(message_data)
 
-        rcpt_to = envelope.rcpt_tos[0].lower()
-
-        # Reply case
-        # reply+ or ra+ (reverse-alias) prefix
-        if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
-            LOG.debug("Reply phase")
-            app = new_app()
-
-            with app.app_context():
-                return self.handle_reply(envelope, smtp, msg)
-        else:  # Forward case
-            LOG.debug("Forward phase")
-            app = new_app()
-
-            with app.app_context():
-                return self.handle_forward(envelope, smtp, msg)
-
-    def handle_forward(self, envelope, smtp: SMTP, msg: Message) -> str:
+        for rcpt_to in envelope.rcpt_tos:
+            # Reply case
+            # reply+ or ra+ (reverse-alias) prefix
+            if rcpt_to.startswith("reply+") or rcpt_to.startswith("ra+"):
+                LOG.debug("Reply phase")
+                app = new_app()
+
+                with app.app_context():
+                    return self.handle_reply(envelope, smtp, msg, rcpt_to)
+            else:  # Forward case
+                LOG.debug("Forward phase")
+                app = new_app()
+
+                with app.app_context():
+                    return self.handle_forward(envelope, smtp, msg, rcpt_to)
+
+    def handle_forward(self, envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str:
         """return *status_code message*"""
-        alias = envelope.rcpt_tos[0].lower()  # alias@SL
+        alias = rcpt_to.lower()  # alias@SL
 
         gen_email = GenEmail.get_by(email=alias)
         if not gen_email:
@@ -325,8 +324,8 @@ class MailHandler:
         db.session.commit()
         return "250 Message accepted for delivery"
 
-    def handle_reply(self, envelope, smtp: SMTP, msg: Message) -> str:
-        reply_email = envelope.rcpt_tos[0].lower()
+    def handle_reply(self, envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> str:
+        reply_email = rcpt_to.lower()
 
         # reply_email must end with EMAIL_DOMAIN
         if not reply_email.endswith(EMAIL_DOMAIN):