示例#1
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)
示例#2
0
def test_read_bulk_contents(monkeypatch, capsys):
    monkeypatch.setattr('sys.stdin', ('key=val', '\n', 'key2=val2', 'a comment'))
    assert utils.read_bulk_contexts() == ({'key': 'val'}, [{'key2': 'val2'}])
    assert capsys.readouterr().err == "Invalid line 'a comment' in bulked notification context\n"
def test_read_bulk_contents(monkeypatch, capsys):
    monkeypatch.setattr("sys.stdin",
                        ("key=val", "\n", "key2=val2", "a comment"))
    assert utils.read_bulk_contexts() == ({"key": "val"}, [{"key2": "val2"}])
    assert capsys.readouterr(
    ).err == "Invalid line 'a comment' in bulked notification context\n"