Ejemplo n.º 1
0
def send_mails(data):
    maintext = "\n".join((str(t) for t in sorted(data)))
    with open("emails.txt") as f:
        mails = [line.strip() for line in f]
    with open(conf.get("DEFAULT", "email_template")) as f:
        tmpl = f.read()
    # unpacks all variables from the general config and passes them as key-value pairs
    body = tmpl.format(
        list=maintext,
        **{k: v
           for d in map(dict,
                        dict(conf).values()) for k, v in d.items()})

    host, usr, pwd, sender = itemgetter("smtp_server", "smtp_username",
                                        "smtp_pwd",
                                        "sender_email")(credentials["email"])
    mailsender = MailSender(host, usr, pwd, sender)
    print(body)
    print(f"\nSending {len(mails)} Emails", end="")
    try:
        with mailsender:
            for mail in mails:
                mailsender.sendmail(mail, body)
    except SMTPAuthenticationError:
        error("EmailAuthentication Failed. No emails sent.")
    except socket.gaierror:
        warning("No network connection")
    print("\rEmails sent   ")