From 601857a2d4d8e14c60aef9ee5e19ed7f509dd178 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 12 Jun 2024 14:04:11 +0200 Subject: [PATCH] email: remove duplicated in file tester entrypoint --- mylib/email.py | 186 ------------------------------------------------- 1 file changed, 186 deletions(-) diff --git a/mylib/email.py b/mylib/email.py index ee81a2c..4908987 100644 --- a/mylib/email.py +++ b/mylib/email.py @@ -433,189 +433,3 @@ class EmailClient( server.quit() return not error - - -if __name__ == "__main__": - # Run tests - import argparse - import datetime - import sys - - # Options parser - parser = argparse.ArgumentParser() - - parser.add_argument( - "-v", "--verbose", action="store_true", dest="verbose", help="Enable verbose mode" - ) - - parser.add_argument( - "-d", "--debug", action="store_true", dest="debug", help="Enable debug mode" - ) - - parser.add_argument( - "-l", "--log-file", action="store", type=str, dest="logfile", help="Log file path" - ) - - parser.add_argument( - "-j", "--just-try", action="store_true", dest="just_try", help="Enable just-try mode" - ) - - email_opts = parser.add_argument_group("Email options") - - email_opts.add_argument( - "-H", "--smtp-host", action="store", type=str, dest="email_smtp_host", help="SMTP host" - ) - - email_opts.add_argument( - "-P", "--smtp-port", action="store", type=int, dest="email_smtp_port", help="SMTP port" - ) - - email_opts.add_argument( - "-S", "--smtp-ssl", action="store_true", dest="email_smtp_ssl", help="Use SSL" - ) - - email_opts.add_argument( - "-T", "--smtp-tls", action="store_true", dest="email_smtp_tls", help="Use TLS" - ) - - email_opts.add_argument( - "-u", "--smtp-user", action="store", type=str, dest="email_smtp_user", help="SMTP username" - ) - - email_opts.add_argument( - "-p", - "--smtp-password", - action="store", - type=str, - dest="email_smtp_password", - help="SMTP password", - ) - - email_opts.add_argument( - "-D", - "--smtp-debug", - action="store_true", - dest="email_smtp_debug", - help="Debug SMTP connection", - ) - - email_opts.add_argument( - "-e", - "--email-encoding", - action="store", - type=str, - dest="email_encoding", - help="SMTP encoding", - ) - - email_opts.add_argument( - "-f", - "--sender-name", - action="store", - type=str, - dest="email_sender_name", - help="Sender name", - ) - - email_opts.add_argument( - "-F", - "--sender-email", - action="store", - type=str, - dest="email_sender_email", - help="Sender email", - ) - - email_opts.add_argument( - "-C", - "--catch-all", - action="store", - type=str, - dest="email_catch_all", - help="Catch all sent email: specify catch recipient email address", - ) - - test_opts = parser.add_argument_group("Test email options") - - test_opts.add_argument( - "-t", - "--to", - action="store", - type=str, - dest="test_to", - help="Test email recipient", - ) - - test_opts.add_argument( - "-m", - "--mako", - action="store_true", - dest="test_mako", - help="Test mako templating", - ) - - options = parser.parse_args() - - if not options.test_to: - parser.error("You must specify test email recipient using -t/--to parameter") - sys.exit(1) - - # Initialize logs - logformat = "%(asctime)s - Test EmailClient - %(levelname)s - %(message)s" - if options.debug: - loglevel = logging.DEBUG - elif options.verbose: - loglevel = logging.INFO - else: - loglevel = logging.WARNING - - if options.logfile: - logging.basicConfig(filename=options.logfile, level=loglevel, format=logformat) - else: - logging.basicConfig(level=loglevel, format=logformat) - - if options.email_smtp_user and not options.email_smtp_password: - import getpass - - options.email_smtp_password = getpass.getpass("Please enter SMTP password: ") - - logging.info("Initialize Email client") - email_client = EmailClient( - smtp_host=options.email_smtp_host, - smtp_port=options.email_smtp_port, - smtp_ssl=options.email_smtp_ssl, - smtp_tls=options.email_smtp_tls, - smtp_user=options.email_smtp_user, - smtp_password=options.email_smtp_password, - smtp_debug=options.email_smtp_debug, - sender_name=options.email_sender_name, - sender_email=options.email_sender_email, - catch_all_addr=options.email_catch_all, - just_try=options.just_try, - encoding=options.email_encoding, - templates={ - "test": { - "subject": "Test email", - "text": ( - "Just a test email sent at {sent_date}." - if not options.test_mako - else MakoTemplate("Just a test email sent at ${sent_date | h}.") # nosec - ), - "html": ( - "Just a test email. (sent at {sent_date | h})" - if not options.test_mako - else MakoTemplate( # nosec - "Just a test email. " - "(sent at ${sent_date | h})" - ) - ), - } - }, - ) - - logging.info("Send a test email to %s", options.test_to) - if email_client.send(options.test_to, template="test", sent_date=datetime.datetime.now()): - logging.info("Test email sent") - sys.exit(0) - logging.error("Fail to send test email") - sys.exit(1)