Example #1
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(to_str(content), 
         subtype, 'utf-8')
     else:
         # Encode non-text attachments with base64.
         attachment = MIMEBase(basetype, subtype)
         attachment.set_payload(content)
         Encoders.encode_base64(attachment)
     if filename:
         attachment.add_header('Content-Disposition', 'attachment',
                               filename=filename)
     return attachment
Example #2
0
 def message(self):
     encoding = self.encoding or 'utf-8'
     msg = SafeMIMEText(to_str(self.body),
                        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.from_email
     msg['To'] = ', '.join(self.to)
     msg['Date'] = formatdate()
     msg['Message-ID'] = make_msgid()
     for name, value in self.extra_headers.items():
         msg[name] = value
     return msg