def _create_attachment(self, filename, content, mimetype=None): """ Converts the filename, content, mimetype triple into a MIME attachment object. Use self.encoding when handling text attachments. """ if mimetype is None: mimetype, _ = mimetypes.guess_type(filename) if mimetype is None: mimetype = constants.MIME_TYPE_EXCEL basetype, subtype = mimetype.split('/', 1) if basetype == 'text': encoding = self.encoding or settings.DEFAULT_CHARSET attachment = SafeMIMEText( smart_str(content, settings.DEFAULT_CHARSET), subtype, encoding) else: # Encode non-text attachments with base64. attachment = MIMEBase(basetype, subtype) attachment.set_payload(content) encoders.encode_base64(attachment) if filename: try: filename = filename.encode('ascii') except UnicodeEncodeError: filename = Header(filename, 'utf-8').encode() attachment.add_header('Content-Disposition', 'attachment', filename=filename) return attachment
def _create_attachment(self, filename, content, mimetype=None): """ Converts the filename, content, mimetype triple into a MIME attachment object. Use self.encoding when handling text attachments. """ 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': encoding = self.encoding or settings.DEFAULT_CHARSET attachment = SafeMIMEText(smart_str(content, settings.DEFAULT_CHARSET), subtype, encoding) else: # Encode non-text attachments with base64. attachment = MIMEBase(basetype, subtype) attachment.set_payload(content) encoders.encode_base64(attachment) if filename: try: filename = filename.encode('ascii') except UnicodeEncodeError: filename = Header(filename, 'utf-8').encode() attachment.add_header('Content-Disposition', 'attachment', filename=filename) return attachment
def build_version_attachment(): version_attachment = SafeMIMEText('Version: 1\n', self.content_subtype, encoding) del version_attachment['Content-Type'] version_attachment.add_header('Content-Type', 'application/pgp-encrypted') version_attachment.add_header('Content-Description', 'PGP/MIME Versions Identification') return version_attachment
def build_gpg_attachment(): gpg_attachment = SafeMIMEText(encrypted_msg, self.content_subtype, encoding) del gpg_attachment['Content-Type'] gpg_attachment.add_header('Content-Type', 'application/octet-stream', name=self.gpg_attachment_filename) gpg_attachment.add_header('Content-Disposition', 'inline', filename=self.gpg_attachment_filename) gpg_attachment.add_header('Content-Description', 'OpenPGP encrypted message') return gpg_attachment