Exemplo n.º 1
0
    def send(self):
        msg = EmailMessage()
        msg = self.set_headers(msg, self.options['email'], self._emailheaders)

        if self.errors:
            error_msg = '\n\n'.join([str(x) for x in self.errors])
            body = self.options['email'].get('body_error',
                                             self.default_body_error)
            msg.set_content(arbiter.parse_string(body, errors=error_msg))
        else:
            data = []
            files = [x for x in self.files if x and not x.startswith('data:')]

            if self.options.get('embed_data_resource', True):
                data = [x for x in self.files if x and x.startswith('data:')]

            subs = {
                'STYLE': self.options.get('style', ''),
                'SUBJECT':
                arbiter.parse_string(self.options['email']['subject'])
            }
            body = self.get_body(self.options['email']['body'], data)

            if self.any_html(data) and self.options.get('html', False):
                p = self.options['email'].get(
                    'html_prologue',
                    arbiter.parse_string(EMAIL_HTML_HEAD, **subs))
                e = self.options['email'].get('html_epilogue', EMAIL_HTML_FOOT)
                msg.set_content(f"{p}{body}{e}", subtype='html')
            else:
                msg.set_content(body)

            if files:
                msg = self.attach_files(msg, files)

        if self.options['smtp'].get('ssl', False):
            klass = smtplib.SMTP_SSL
        elif self.options['smtp'].get('lmtp', False):
            klass = smtplib.LMTP
        else:
            klass = smtplib.SMTP

        _smtp_opts = self._EmailHandler__smtp_options()

        with klass(host=self.options['smtp']['host'], **_smtp_opts) as smtp:
            if self.options['smtp'].get('tls', False):
                tlsargs = {
                    x: self.options['smtp'][x]
                    for x in self.options['smtp']
                    if x in ['keyfile', 'certfile']
                }
                smtp.starttls(**tlsargs)

            if self.options['smtp'].get('username', None) \
            and self.options['smtp'].get('password', None):
                smtp.login(self.options['smtp']['username'],
                           self.options['smtp']['password'])

            smtp.send_message(msg)
Exemplo n.º 2
0
    def set_headers(msg, options, headers):
        for k in options:
            if k in headers:
                if isinstance(options[k], list):
                    msg[k] = arbiter.parse_string(COMMASPACE.join(options[k]))
                else:
                    msg[k] = arbiter.parse_string(options[k])

        return msg
Exemplo n.º 3
0
    def get_body(body, resources):
        _data = ''

        for res in resources:
            _data += ExtendedEmailHandler.decode(res)

        return arbiter.parse_string(body, data=_data)
Exemplo n.º 4
0
    def __init__(self, config, **kwargs):
        super().__init__()

        self.config = config
        self.options = config.get('options', {})
        self.authentication = config.get('authentication', None)
        self.resource = arbiter.parse_string(config.get('resource', None))

        if kwargs:
            for k in kwargs:
                self.options[k] = kwargs[k]
Exemplo n.º 5
0
    def send(self):
        from email.message import EmailMessage
        from email.utils import COMMASPACE
        import mimetypes
        import smtplib

        msg = EmailMessage()

        if self.errors:
            error_msg = '\n\n'.join([str(x) for x in self.errors])

            if 'body_error' not in self.options['email']:
                body = self.default_body_error
            else:
                body = self.options['email']['body_error']

            msg.set_content(
                arbiter.parse_string(body, errors=' '.join(error_msg)))
        else:
            msg.set_content(arbiter.parse_string(
                self.options['email']['body']))

        # write headers
        for k in self.options['email']:
            if k in self._emailheaders:
                if isinstance(self.options['email'][k], list):
                    msg[k] = arbiter.parse_string(
                        COMMASPACE.join(self.options['email'][k]))
                else:
                    msg[k] = arbiter.parse_string(self.options['email'][k])

        # attach files
        for file in self.files:
            ctype, encoding = mimetypes.guess_type(file)

            # unknown, treat as binary
            if ctype is None or encoding is not None:
                ctype = 'application/octet-stream'

            maintype, subtype = ctype.split('/', 1)

            with open(file, 'rb') as fp:
                msg.add_attachment(fp.read(),
                                   maintype=maintype,
                                   subtype=subtype,
                                   filename=os.path.basename(file))

        if self.options['smtp'].get('ssl', False):
            klass = smtplib.SMTP_SSL
        elif self.options['smtp'].get('lmtp', False):
            klass = smtplib.LMTP
        else:
            klass = smtplib.SMTP

        with klass(host=self.options['smtp']['host'],
                   **self.__smtp_options()) as smtp:
            if self.options['smtp'].get('tls', False):
                tlsargs = {
                    x: self.options['smtp'][x]
                    for x in self.options['smtp']
                    if x in ['keyfile', 'certfile']
                }
                smtp.starttls(**tlsargs)

            if self.options['smtp'].get('username', None) \
            and self.options['smtp'].get('password', None):
                smtp.login(self.options['smtp']['username'],
                           self.options['smtp']['password'])

            smtp.send_message(msg)