def test_get_smtp_password(self): # Given password config = {"app": {"smtp": {"password": "******"}}} self.assertEqual( config["app"]["smtp"]["password"], config_parser.get_smtp_password(config=config), )
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
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_password_invalids(self): config = config_parser.read_config() self.assertIsNone(config_parser.get_smtp_password(config=None))
def test_get_smtp_password_valids(self): # Given password config = {'smtp': {'password': '******'}} self.assertEqual(config['smtp']['password'], config_parser.get_smtp_password(config=config))
def test_smtp_password_none_config(self): self.assertIsNone(config_parser.get_smtp_password(config=None))