示例#1
0
文件: mail.py 项目: mdagosta/hydra
def render_email(handler, from_email, to_addrs, subject, template, email_opts, **kwargs):
    if not handler:
        template_loader = tornado.template.Loader(options.template_path)
        plain = stdlib.unicode_str(template_loader.load(template+'.txt').generate(**email_opts))
        html = stdlib.unicode_str(template_loader.load(template+'.html').generate(**email_opts))
    else:
        plain = handler.render_string('%s.txt' % template, **email_opts)
        html =  handler.render_string('%s.html' % template, **email_opts)
    body = {'plain': plain, 'html': html}
    reply_to = kwargs.get('reply_to', from_email)
    service_sender = kwargs.get('service_sender', from_email)
    return encode_email(from_email, to_addrs, subject, body, reply_to, service_sender)
示例#2
0
def render_email(handler, from_email, to_addrs, subject, template, email_opts,
                 **kwargs):
    if not handler:
        template_loader = tornado.template.Loader(options.template_path)
        plain = stdlib.unicode_str(
            template_loader.load(template + '.txt').generate(**email_opts))
        html = stdlib.unicode_str(
            template_loader.load(template + '.html').generate(**email_opts))
    else:
        plain = handler.render_string('%s.txt' % template, **email_opts)
        html = handler.render_string('%s.html' % template, **email_opts)
    body = {'plain': plain, 'html': html}
    reply_to = kwargs.get('reply_to', from_email)
    service_sender = kwargs.get('service_sender', from_email)
    return encode_email(from_email, to_addrs, subject, body, reply_to,
                        service_sender)
示例#3
0
文件: mail.py 项目: mdagosta/hydra
def mime(txt, subtype):
    if txt is None:
        return None
    for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8':
        try:
            txt.encode(body_charset)
        except UnicodeError:
            pass
        else:
            break
    charset = body_charset
    if charset == 'UTF-8':
        txt = stdlib.unicode_str(txt, charset)
    return email.mime.text.MIMEText(txt.encode(charset), subtype, charset)
示例#4
0
def mime(txt, subtype):
    if txt is None:
        return None
    for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8':
        try:
            txt.encode(body_charset)
        except UnicodeError:
            pass
        else:
            break
    charset = body_charset
    if charset == 'UTF-8':
        txt = stdlib.unicode_str(txt, charset)
    return email.mime.text.MIMEText(txt.encode(charset), subtype, charset)
示例#5
0
def encode_email(sender, recipient, subject, body, reply_to, service_sender):
    plain, html = None, None
    if isinstance(body, basestring):
        plain = body
    else:
        plain = body.get('plain', None)
        html = body.get('html', None)
    header_charset = 'utf-8'
    parts = []
    if plain:
        parts.append(mime(plain, 'plain'))
    if html:
        parts.append(mime(html, 'html'))
    if len(parts) > 1:
        msg = email.mime.multipart.MIMEMultipart('alternative')
        for p in parts:
            msg.attach(p)
    else:
        msg = parts[0]
    # Set up headers
    headers = (('To', recipient), ('From', sender), ('Reply-to', reply_to),
               ('Sender', service_sender))
    for header in headers:
        if not header[1]:
            continue
        name, addr = email.Utils.parseaddr(header[1])
        name = str(email.Header.Header(unicode(name), header_charset))
        addr = addr.encode('ascii')
        if header[0] == 'To' and name:
            msg[header[0]] = '"%s" <%s>' % (name.replace('"', "'"), addr)
        else:
            msg[header[0]] = email.Utils.formataddr((name, addr))
    # Finish message
    subject = stdlib.unicode_str(subject, header_charset)
    msg['Subject'] = email.Header.Header(stdlib.unicode_str(subject),
                                         header_charset)
    return msg.as_string()
示例#6
0
文件: mail.py 项目: mdagosta/hydra
def encode_email(sender, recipient, subject, body, reply_to, service_sender):
    plain, html = None, None
    if isinstance(body, basestring):
        plain = body
    else:
        plain = body.get('plain', None)
        html = body.get('html', None)
    header_charset = 'utf-8'
    parts = []
    if plain:
        parts.append(mime(plain, 'plain'))
    if html:
        parts.append(mime(html, 'html'))
    if len(parts) > 1:
        msg = email.mime.multipart.MIMEMultipart('alternative')
        for p in parts:
            msg.attach(p)
    else:
        msg = parts[0]
    # Set up headers
    headers = (('To', recipient), ('From', sender), ('Reply-to', reply_to),
               ('Sender', service_sender))
    for header in headers:
        if not header[1]:
            continue
        name, addr = email.Utils.parseaddr(header[1])
        name = str(email.Header.Header(unicode(name), header_charset))
        addr = addr.encode('ascii')
        if header[0] == 'To' and name:
            msg[header[0]] = '"%s" <%s>' % (name.replace('"', "'"), addr)
        else:
            msg[header[0]] = email.Utils.formataddr((name, addr))
    # Finish message
    subject = stdlib.unicode_str(subject, header_charset)
    msg['Subject'] = email.Header.Header(stdlib.unicode_str(subject), header_charset)
    return msg.as_string()