all repos — nasg @ ce9ddb451a9f7cd9f27a2cb67c43b8ce459d611f

envelope.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8 :

__author__ = "Peter Molnar"
__copyright__ = "Copyright 2017-2018, Peter Molnar"
__license__ = "GPLv3"
__version__ = "2.1.0"
__maintainer__ = "Peter Molnar"
__email__ = "mail@petermolnar.net"
__status__ = "Production"

"""
    fancy email module of NASG
    Copyright (C) 2017-2018 Peter Molnar

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
"""


from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
import email.charset
from email.generator import Generator
from io import StringIO
import mimetypes
from email.mime.base import MIMEBase
from email.encoders import encode_base64
import email.utils

import time
import getpass
import socket
import shutil
import requests
import tempfile
import atexit
import os
import re
import smtplib
import logging
from shared import Pandoc

class Letter(object):
    def __init__(self, sender=None, recipient=None, subject='', text=''):
        self.sender = sender or (getpass.getuser(), socket.gethostname())
        self.recipient = recipient or self.sender

        self.tmp = tempfile.mkdtemp(
            'envelope_',
            dir=tempfile.gettempdir()
        )
        atexit.register(
            shutil.rmtree,
            os.path.abspath(self.tmp)
        )
        self.text = text;
        self.subject = subject
        self.images = []
        self.ready = None
        self.time = time.time()
        self.headers = {}

    @property
    def _html(self):
        return Pandoc().convert(self.text)

    @property
    def _tmpl(self):
        return "<html><head></head><body>%s</body></html>" % (self._html)

    def __pull_image(self, img):
        fname = os.path.basename(img)
        i = {
            'url': img,
            'name': fname,
            'tmp': os.path.join(self.tmp, fname),
        }

        logging.debug("pulling image %s", i['url'])
        r = requests.get(i['url'], stream=True)
        if r.status_code == 200:
            with open(i['tmp'], 'wb') as f:
                logging.debug("writing image %s", i['tmp'])
                r.raw.decode_content = True
                shutil.copyfileobj(r.raw, f)
                if not isinstance(self.images, list):
                    self.images = []
                self.images.append(i)


    def __pull_images(self):
        mdmatch = re.compile(
            r'!\[.*\]\((.*?\.(?:jpe?g|png|gif)(?:\s+[\'\"]?.*?[\'\"]?)?)\)'
            r'(?:\{.*?\})?'
        )
        [self.__pull_image(img) for img in mdmatch.findall(self.text)]


    def __attach_images(self):
        self.__pull_images()
        for i in self.images:
            cid = 'cid:%s' % (i['name'])
            logging.debug("replacing %s with %s", i['url'], cid)
            self.text = self.text.replace(i['url'], cid)


    def make(self, inline_images=True):
        if inline_images:
            self.__attach_images()


        # Python, by default, encodes utf-8 in base64, which makes plain text
        # mail painful; this overrides and forces Quoted Printable.
        # Quoted Printable is still awful, but better, and we're going to
        # force the mail to be 8bit encoded.
        # Note: enforcing 8bit breaks compatibility with ancient mail clients.
        email.charset.add_charset('utf-8', email.charset.QP, email.charset.QP, 'utf-8')

        mail = MIMEMultipart('alternative')

        # --- setting headers ---
        self.headers = {
            'Subject': Header(re.sub(r"\r?\n?$", "", self.subject, 1), 'utf-8').encode(),
            'To': email.utils.formataddr(self.recipient),
            'From': email.utils.formataddr(self.sender),
            'Date': email.utils.formatdate(self.time, localtime=True)
        }

        for k, v in self.headers.items():
            mail.add_header(k, "%s" % v)
        logging.debug("headers: %s", self.headers)

        # --- adding plain text ---
        text = self.text
        _text = MIMEText(text, 'text', _charset='utf-8')
        # ---
        # this is the part where we overwrite the way Python thinks:
        # force the text to be the actual, unencoded, utf-8.
        # Note:these steps breaks compatibility with ancient mail clients.
        _text.replace_header('Content-Transfer-Encoding', '8bit')
        _text.replace_header('Content-Type', 'text/plain; charset=utf-8')
        _text.set_payload(self.text)
        # ---
        logging.debug("text: %s", _text)
        mail.attach(_text)

        # --- HTML bit ---
        # this is where it gets tricky: the HTML part should be a 'related'
        # wrapper, in which the text and all the related images are sitting
        _envelope = MIMEMultipart('related')


        html = self._tmpl
        _html = MIMEText(html, 'html', _charset='utf-8')
        # ---
        # see above under 'adding plain text'
        _html.replace_header('Content-Transfer-Encoding', '8bit')
        _html.replace_header('Content-Type', 'text/html; charset=utf-8')
        _html.set_payload(html)
        # ---
        logging.debug("HTML: %s", _html)
        _envelope.attach(_html)

        for i in self.images:
            mimetype, encoding = mimetypes.guess_type(i['tmp'])
            mimetype = mimetype or 'application/octet-stream'
            mimetype = mimetype.split('/', 1)
            attachment = MIMEBase(mimetype[0], mimetype[1])
            with open(i['tmp'], 'rb') as img:
                attachment.set_payload(img.read())
                img.close()
            os.unlink(i['tmp'])

            encode_base64(attachment)
            attachment.add_header(
                'Content-Disposition',
                'inline',
                filename=i['name']
            )
            attachment.add_header(
                'Content-ID',
                '<%s>' % (i['name'])
            )

            _envelope.attach(attachment)

        # add the whole html + image pack to the mail
        mail.attach(_envelope)

        str_io = StringIO()
        g = Generator(str_io, False)
        g.flatten(mail)

        self.ready = str_io.getvalue().encode('utf-8')

    def send(self):
        if not self.ready:
            logging.error('this mail is not ready')
            return

        try:
            s = smtplib.SMTP('127.0.0.1', 25)
            # unless you do the encode, you'll get:
            #   File "/usr/local/lib/python3.5/smtplib.py", line 850, in sendmail
            #   msg = _fix_eols(msg).encode('ascii')
            #   UnicodeEncodeError: 'ascii' codec can't encode character '\xa0' in position 1073: ordinal not in range(128)
            s.sendmail(self.headers['From'], self.headers['To'], self.ready)
            s.quit()
        except Exception as e:
            logging.error('sending mail failed with error: %s', e)