Changeset - 29e10c6a1551
[Not reviewed]
0 1 0
Lance Edgar (lance) - 7 years ago 2018-02-22 13:15:54
lance@edbob.org
Add email attachment MIME type for MS Word .doc files
1 file changed with 1 insertions and 0 deletions:
0 comments (0 inline, 0 general)
rattail/mail.py
Show inline comments
 
@@ -422,48 +422,49 @@ class Email(object):
 

	
 
    def get_template(self, type_):
 
        """
 
        Locate and return the Mako email template of the given type
 
        (e.g. 'html'), or ``None`` if no such template can be found.
 
        """
 
        try:
 
            return self.templates.get_template('{0}.{1}.mako'.format(self.key, type_))
 
        except TopLevelLookupException:
 
            if self.fallback_key:
 
                try:
 
                    return self.templates.get_template('{0}.{1}.mako'.format(self.fallback_key, type_))
 
                except TopLevelLookupException:
 
                    pass
 

	
 
    def normalize_attachments(self, attachments):
 
        normalized = []
 
        for attachment in attachments:
 
            if isinstance(attachment, six.string_types):
 
                attachment = self.normalize_attachment(attachment)
 
            normalized.append(attachment)
 
        return normalized
 

	
 
    ATTACHMENT_MIME_MAP = {
 
        '.doc': 'application/msword',
 
        '.pdf': 'pdf',
 
        '.xls': 'vnd.ms-excel',
 
    }
 

	
 
    def normalize_attachment(self, path):
 
        root, ext = os.path.splitext(path)
 
        ext = ext.lower()
 
        if ext == '.csv':
 
            with open(path, 'rb') as f:
 
                part = MIMEText(f.read(), 'csv', 'utf_8')
 
            filename = os.path.basename(path)
 
            part.add_header('Content-Disposition', 'attachment; filename="{}"'.format(filename))
 
            return part
 
        else:
 
            mimetype = self.ATTACHMENT_MIME_MAP.get(ext)
 
            if mimetype:
 
                with open(path, 'rb') as f:
 
                    part = MIMEApplication(f.read(), mimetype)
 
                filename = os.path.basename(path)
 
                part.add_header('Content-Disposition', 'attachment; filename="{}"'.format(filename))
 
                return part
 
        raise ValueError("Magic is not (yet) supported, please construct your own attachments for file: {}".format(path))
 

	
 
    def make_message(self, data={}, attachments=[], inlines=[],
0 comments (0 inline, 0 general)