소스 검색

add: option to add custom email headers

Mikael Koli 3 년 전
부모
커밋
a293ae910a
1개의 변경된 파일18개의 추가작업 그리고 1개의 파일을 삭제
  1. 18 1
      redmail/email/sender.py

+ 18 - 1
redmail/email/sender.py

@@ -158,6 +158,7 @@ class EmailSender:
         self.cc = None
         self.cc = None
         self.bcc = None
         self.bcc = None
         self.subject = None
         self.subject = None
+        self.headers = None
 
 
         self.text = None
         self.text = None
         self.html = None
         self.html = None
@@ -177,6 +178,7 @@ class EmailSender:
              receivers:Union[List[str], str, None]=None,
              receivers:Union[List[str], str, None]=None,
              cc:Union[List[str], str, None]=None,
              cc:Union[List[str], str, None]=None,
              bcc:Union[List[str], str, None]=None,
              bcc:Union[List[str], str, None]=None,
+             headers:Optional[Dict[str, str]]=None,
              html:Optional[str]=None,
              html:Optional[str]=None,
              text:Optional[str]=None,
              text:Optional[str]=None,
              html_template:Optional[str]=None,
              html_template:Optional[str]=None,
@@ -205,6 +207,9 @@ class EmailSender:
             Blind Carbon Copy of the email.
             Blind Carbon Copy of the email.
             Additional recipients of the email that
             Additional recipients of the email that
             don't see who else got the email.
             don't see who else got the email.
+        headers : dict, optional
+            Additional email headers. Can be used to 
+            override already set headers.
         html : str, optional
         html : str, optional
             HTML body of the email. This is processed
             HTML body of the email. This is processed
             by Jinja and may contain loops, parametrization
             by Jinja and may contain loops, parametrization
@@ -280,6 +285,7 @@ class EmailSender:
             receivers=receivers,
             receivers=receivers,
             cc=cc,
             cc=cc,
             bcc=bcc,
             bcc=bcc,
+            headers=headers,
             html=html,
             html=html,
             text=text,
             text=text,
             html_template=html_template,
             html_template=html_template,
@@ -306,6 +312,7 @@ class EmailSender:
                   body_tables:Optional[Dict[str, 'pd.DataFrame']]=None, 
                   body_tables:Optional[Dict[str, 'pd.DataFrame']]=None, 
                   body_params:Optional[Dict[str, Any]]=None,
                   body_params:Optional[Dict[str, Any]]=None,
                   attachments:Optional[Dict[str, Union[str, os.PathLike, 'pd.DataFrame', bytes]]]=None,
                   attachments:Optional[Dict[str, Union[str, os.PathLike, 'pd.DataFrame', bytes]]]=None,
+                  headers:Optional[Dict[str, str]]=None,
                   use_jinja=None) -> EmailMessage:
                   use_jinja=None) -> EmailMessage:
         """Get the email message"""
         """Get the email message"""
 
 
@@ -315,6 +322,7 @@ class EmailSender:
         receivers = self.get_receivers(receivers)
         receivers = self.get_receivers(receivers)
         cc = self.get_cc(cc)
         cc = self.get_cc(cc)
         bcc = self.get_bcc(bcc)
         bcc = self.get_bcc(bcc)
+        headers = self.get_headers(headers)
 
 
         html = html or self.html
         html = html or self.html
         text = text or self.text
         text = text or self.text
@@ -331,6 +339,7 @@ class EmailSender:
             receivers=receivers,
             receivers=receivers,
             cc=cc,
             cc=cc,
             bcc=bcc,
             bcc=bcc,
+            headers=headers 
         )
         )
         has_text = text is not None or text_template is not None
         has_text = text is not None or text_template is not None
         has_html = html is not None or html_template is not None
         has_html = html is not None or html_template is not None
@@ -388,6 +397,10 @@ class EmailSender:
         """Get blind carbon copy (bcc) of the email"""
         """Get blind carbon copy (bcc) of the email"""
         return bcc or self.bcc
         return bcc or self.bcc
 
 
+    def get_headers(self, headers:Union[Dict[str, str], None]):
+        """Get additional headers"""
+        return headers or self.headers
+
     def get_sender(self, sender:Union[str, None]) -> str:
     def get_sender(self, sender:Union[str, None]) -> str:
         """Get sender of the email"""
         """Get sender of the email"""
         return sender or self.sender or self.username
         return sender or self.sender or self.username
@@ -398,7 +411,7 @@ class EmailSender:
             domain = sender.split("@")[1]
             domain = sender.split("@")[1]
         return make_msgid(domain=domain)
         return make_msgid(domain=domain)
 
 
-    def _create_body(self, subject, sender, receivers=None, cc=None, bcc=None) -> EmailMessage:
+    def _create_body(self, subject, sender, receivers=None, cc=None, bcc=None, headers=None) -> EmailMessage:
         msg = EmailMessage()
         msg = EmailMessage()
         msg["From"] = sender
         msg["From"] = sender
         msg["Subject"] = subject
         msg["Subject"] = subject
@@ -415,6 +428,10 @@ class EmailSender:
         # or the program sending the email (as we are doing now).
         # or the program sending the email (as we are doing now).
         # Apparently Gmail might require it as of 2022
         # Apparently Gmail might require it as of 2022
         msg['Message-ID'] = self.create_message_id(sender)
         msg['Message-ID'] = self.create_message_id(sender)
+
+        if headers:
+            for key, val in headers.items():
+                msg[key] = val
         return msg
         return msg
 
 
     def _set_content_type(self, msg:EmailMessage, has_text, has_html, has_attachments):
     def _set_content_type(self, msg:EmailMessage, has_text, has_html, has_attachments):