Esempio n. 1
0
def send(config, last_send=None, dry_run=False):
    sent_on = None
    email = config_parser.get_smtp_email(config=config)
    host = config_parser.get_smtp_host(config=config)
    port = config_parser.get_smtp_port(config=config)
    no_tls = config_parser.get_smtp_no_tls(config=config)
    password = config_parser.get_smtp_password(config=config)

    if (last_send and last_send >
            datetime.datetime.now() - datetime.timedelta(hours=24)):
        print("Throttling email to once a day")
        sent_on = last_send
    elif email and host and port:
        try:
            sent_on = datetime.datetime.now()
            if not dry_run:
                smtp = smtplib.SMTP(host, port)
                smtp.set_debuglevel(0)
                smtp.connect(host, port)
                if not no_tls:
                    smtp.starttls()

                if password:
                    smtp.login(email, password)

                msg = build_message(email)

                smtp.sendmail(email, email, msg.as_string())
                smtp.quit()
        except (Exception) as e:
            sent_on = None
            print("Error: failed to send email:" + str(e))
            logging.exception(e)
    else:
        print("Not sending 2FA notification because SMTP is not configured")

    return sent_on
Esempio n. 2
0
def send(config, last_send=None, dry_run=False):
    sent_on = None
    email = config_parser.get_smtp_email(config=config)
    to_email = config_parser.get_smtp_to_email(config=config)
    host = config_parser.get_smtp_host(config=config)
    port = config_parser.get_smtp_port(config=config)
    no_tls = config_parser.get_smtp_no_tls(config=config)
    password = config_parser.get_smtp_password(config=config)

    if last_send and last_send > datetime.datetime.now() - datetime.timedelta(hours=24):
        LOGGER.info("Throttling email to once a day")
        sent_on = last_send
    elif email and host and port:
        try:
            sent_on = datetime.datetime.now()
            if not dry_run:
                smtp = smtplib.SMTP(host, port)
                smtp.set_debuglevel(0)
                smtp.connect(host, port)
                if not no_tls:
                    smtp.starttls()

                if password:
                    smtp.login(email, password)

                msg = build_message(email)

                smtp.sendmail(from_addr=email, to_addrs=to_email, msg=msg.as_string())
                smtp.quit()
        except (Exception) as e:
            sent_on = None
            LOGGER.error(f"Failed to send email: {str(e)}.")
    else:
        LOGGER.warning("Not sending 2FA notification because SMTP is not configured")

    return sent_on
 def test_smtp_host_invalids(self):
     config = config_parser.read_config()
     self.assertIsNone(config_parser.get_smtp_host(config=None))
 def test_get_smtp_host_valids(self):
     # Given host
     config = {'smtp': {'host': 'smtp.test.com'}}
     self.assertEqual(config['smtp']['host'],
                      config_parser.get_smtp_host(config=config))
Esempio n. 5
0
 def test_smtp_host_none_config(self):
     self.assertIsNone(config_parser.get_smtp_host(config=None))
Esempio n. 6
0
 def test_get_smtp_host(self):
     # Given host
     config = {"app": {"smtp": {"host": "smtp.test.com"}}}
     self.assertEqual(config["app"]["smtp"]["host"],
                      config_parser.get_smtp_host(config=config))