def build_attachments(self): """Build email's attachment messages""" attachments = [] for attachment in self.newsletter.attachment_set.all(): ctype, encoding = mimetypes.guess_type(attachment.file_attachment.path) if ctype is None or encoding is not None: ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) fd = open(attachment.file_attachment.path, 'rb') if maintype == 'text': message_attachment = MIMEText(fd.read(), _subtype=subtype) elif maintype == 'message': message_attachment = message_from_file(fd) elif maintype == 'image': message_attachment = MIMEImage(fd.read(), _subtype=subtype) elif maintype == 'audio': message_attachment = MIMEAudio(fd.read(), _subtype=subtype) else: message_attachment = MIMEBase(maintype, subtype) message_attachment.set_payload(fd.read()) encode_base64(message_attachment) fd.close() message_attachment.add_header('Content-Disposition', 'attachment', filename=attachment.title) attachments.append(message_attachment) return attachments
def build_attachments(self): """Build email's attachment messages""" attachments = [] for attachment in self.newsletter.attachment_set.all(): ctype, encoding = mimetypes.guess_type( attachment.file_attachment.path) if ctype is None or encoding is not None: ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) fd = open(attachment.file_attachment.path, 'rb') if maintype == 'text': message_attachment = MIMEText(fd.read(), _subtype=subtype) elif maintype == 'message': message_attachment = message_from_file(fd) elif maintype == 'image': message_attachment = MIMEImage(fd.read(), _subtype=subtype) elif maintype == 'audio': message_attachment = MIMEAudio(fd.read(), _subtype=subtype) else: message_attachment = MIMEBase(maintype, subtype) message_attachment.set_payload(fd.read()) encode_base64(message_attachment) fd.close() message_attachment.add_header('Content-Disposition', 'attachment', filename=attachment.title) attachments.append(message_attachment) return attachments
def get_attachment(path, filename): ctype, encoding = mimetypes.guess_type(path) if ctype is None or encoding is not None: ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) fp = open(path, 'rb') if maintype == 'text': attachment = MIMEText(fp.read(),_subtype=subtype) elif maintype == 'message': attachment = email.message_from_file(fp) elif maintype == 'image': attachment = MIMEImage(fp.read(),_subtype=subtype) elif maintype == 'audio': attachment = MIMEAudio(fp.read(),_subtype=subtype) else: attachment = MIMEBase(maintype, subtype) attachment.set_payload(fp.read()) encode_base64(attachment) fp.close() attachment.add_header( 'Content-Disposition', 'attachmentment', filename=filename) return attachment