Ejemplo n.º 1
0
def multipart_mail(target, subject, from_address, reply_to, content_txt, content_html, attach=None):
    if attach is None:
        attach = []

    m = MIMEMultipart("related", _charset="utf-8")

    alt = MIMEMultipart("alternative")

    # The plain text part
    txt = MIMEText(content_txt, "plain", _charset="utf-8")
    alt.attach(txt)

    # The html text part
    html = MIMEText(content_html, "html", _charset="utf-8")
    alt.attach(html)

    m.attach(alt)

    # Add all attachments
    for what, name, contents, how in attach:
        part = (
            MIMEImage(contents, name=name)
            if what == "img"  #
            else MIMEApplication(contents, name=name)
        )
        part.add_header("Content-ID", "<%s>" % name)
        # how must be inline or attachment
        part.add_header("Content-Disposition", how, filename=name)
        m.attach(part)

    return utils.set_mail_headers(target, subject, from_address, reply_to, m)
Ejemplo n.º 2
0
def multipart_mail(target, subject, from_address, reply_to, content_txt, content_html, attach=None):
    if attach is None:
        attach = []

    m = MIMEMultipart('related', _charset='utf-8')

    alt = MIMEMultipart('alternative')

    # The plain text part
    txt = MIMEText(content_txt, 'plain', _charset='utf-8')
    alt.attach(txt)

    # The html text part
    html = MIMEText(content_html, 'html', _charset='utf-8')
    alt.attach(html)

    m.attach(alt)

    # Add all attachments
    for what, name, contents, how in attach:
        part = (
            MIMEImage(contents, name=name) if what == 'img'  #
            else MIMEApplication(contents, name=name))
        part.add_header('Content-ID', '<%s>' % name)
        # how must be inline or attachment
        part.add_header('Content-Disposition', how, filename=name)
        m.attach(part)

    return utils.set_mail_headers(target, subject, from_address, reply_to, m)
Ejemplo n.º 3
0
def main():
    if bulk_mode:
        content_txt = ""
        parameters, contexts = utils.read_bulk_contexts()
        hosts = set()
        for context in contexts:
            context.update(parameters)
            content_txt += construct_content(context)
            mailto = context["CONTACTEMAIL"]  # Assume the same in each context
            subject = context["SUBJECT"]
            hosts.add(context["HOSTNAME"])

        # Use the single context subject in case there is only one context in the bulk
        if len(contexts) > 1:
            subject = utils.get_bulk_notification_subject(contexts, hosts)

    else:
        # gather all options from env
        context = utils.collect_context()
        content_txt = construct_content(context)
        mailto = context["CONTACTEMAIL"]
        subject = context["SUBJECT"]

    if not mailto:  # e.g. empty field in user database
        sys.stdout.write(
            "Cannot send ASCII email: empty destination email address\n")
        sys.exit(2)

    # Create the mail and send it
    from_address = utils.format_address(
        context.get("PARAMETER_FROM_DISPLAY_NAME", ""),
        context.get("PARAMETER_FROM_ADDRESS", utils.default_from_address()),
    )
    reply_to = utils.format_address(
        context.get("PARAMETER_REPLY_TO_DISPLAY_NAME", ""),
        context.get("PARAMETER_REPLY_TO_ADDRESS", ""),
    )
    m = utils.set_mail_headers(
        mailto, subject, from_address, reply_to,
        MIMEText(content_txt, "plain", _charset="utf-8"))
    try:
        sys.exit(utils.send_mail_sendmail(m, mailto, from_address))
    except Exception as e:
        sys.stderr.write("Unhandled exception: %s\n" % e)
        # unhandled exception, don't retry this...
        sys.exit(2)