Example #1
0
File: mail.py Project: alefnula/tea
 def message(self):
     encoding = self.encoding or DEFAULT_CHARSET
     msg = SafeMIMEText(
         smart_bytes(self.body, DEFAULT_CHARSET),
         self.content_subtype,
         encoding,
     )
     if self.attachments:
         body_msg = msg
         msg = SafeMIMEMultipart(_subtype=self.multipart_subtype)
         if self.body:
             msg.attach(body_msg)
         for attachment in self.attachments:
             if isinstance(attachment, MIMEBase):
                 msg.attach(attachment)
             else:
                 msg.attach(self._create_attachment(*attachment))
     msg["Subject"] = self.subject
     msg["From"] = self.sender
     msg["To"] = ", ".join(self.to)
     msg["Cc"] = ", ".join(self.cc)
     # Email header names are case-insensitive (RFC 2045), so we have to
     # accommodate that when doing comparisons.
     header_names = [key.lower() for key in self.extra_headers]
     if "date" not in header_names:
         msg["Date"] = formatdate()
     if "message-id" not in header_names:
         msg["Message-ID"] = make_msgid()
     for name, value in self.extra_headers.items():
         msg[name] = value
     return msg
Example #2
0
File: mail.py Project: SenadI/tea
 def message(self):
     encoding = self.encoding or DEFAULT_CHARSET
     msg = SafeMIMEText(smart_bytes(self.body, DEFAULT_CHARSET),
                        self.content_subtype, encoding)
     if self.attachments:
         body_msg = msg
         msg = SafeMIMEMultipart(_subtype=self.multipart_subtype)
         if self.body:
             msg.attach(body_msg)
         for attachment in self.attachments:
             if isinstance(attachment, MIMEBase):
                 msg.attach(attachment)
             else:
                 msg.attach(self._create_attachment(*attachment))
     msg['Subject'] = self.subject
     msg['From'] = self.sender
     msg['To'] = ', '.join(self.to)
     msg['Cc'] = ', '.join(self.cc)
     # Email header names are case-insensitive (RFC 2045), so we have to
     # accommodate that when doing comparisons.
     header_names = [key.lower() for key in self.extra_headers]
     if 'date' not in header_names:
         msg['Date'] = formatdate()
     if 'message-id' not in header_names:
         msg['Message-ID'] = make_msgid()
     for name, value in self.extra_headers.items():
         msg[name] = value
     return msg
Example #3
0
 def _create_attachment(self, filename, content, mimetype=None):
     """Converts the filename, content, mimetype triple into a MIME
     attachment object.
     """
     if mimetype is None:
         mimetype, _ = mimetypes.guess_type(filename)
         if mimetype is None:
             mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
     basetype, subtype = mimetype.split("/", 1)
     if basetype == "text":
         attachment = SafeMIMEText(smart_bytes(content, DEFAULT_CHARSET), subtype, DEFAULT_CHARSET)
     else:
         # Encode non-text attachments with base64.
         attachment = MIMEBase(basetype, subtype)
         attachment.set_payload(content)
         encode_base64(attachment)
     if filename:
         attachment.add_header("Content-Disposition", "attachment", filename=filename)
     return attachment
Example #4
0
File: mail.py Project: SenadI/tea
 def _create_attachment(self, filename, content, mimetype=None):
     """Converts the filename, content, mimetype triple into a MIME
     attachment object.
     """
     if mimetype is None:
         mimetype, _ = mimetypes.guess_type(filename)
         if mimetype is None:
             mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
     basetype, subtype = mimetype.split('/', 1)
     if basetype == 'text':
         attachment = SafeMIMEText(smart_bytes(content, DEFAULT_CHARSET),
                                   subtype, DEFAULT_CHARSET)
     else:
         # Encode non-text attachments with base64.
         attachment = MIMEBase(basetype, subtype)
         attachment.set_payload(content)
         encode_base64(attachment)
     if filename:
         attachment.add_header('Content-Disposition',
                               'attachment',
                               filename=filename)
     return attachment