diff --git a/mylib/scripts/email_test.py b/mylib/scripts/email_test.py index 4af12c7..affb953 100644 --- a/mylib/scripts/email_test.py +++ b/mylib/scripts/email_test.py @@ -53,7 +53,6 @@ def main(argv=None): # pylint: disable=too-many-locals,too-many-statements if options.email_smtp_user and not options.email_smtp_password: options.email_smtp_password = getpass.getpass('Please enter SMTP password: ') - log.info('Initialize Email client') email_client = init_email_client( options, templates=dict( diff --git a/mylib/scripts/email_test_with_config.py b/mylib/scripts/email_test_with_config.py new file mode 100644 index 0000000..639127b --- /dev/null +++ b/mylib/scripts/email_test_with_config.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + +""" Test Email client using mylib.config.Config for configuration """ +import datetime +import logging +import sys + +from mako.template import Template as MakoTemplate + +from mylib.config import Config +from mylib.email import EmailClient + + +log = logging.getLogger(__name__) + + +def main(argv=None): # pylint: disable=too-many-locals,too-many-statements + """ Script main """ + if argv is None: + argv = sys.argv[1:] + + config = Config(__doc__, __name__.replace('.', '_')) + + email_client = EmailClient(config=config) + email_client.configure() + + # Options parser + parser = config.get_arguments_parser(description=__doc__) + + 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 = config.parse_arguments_options() + + if not options.test_to: + parser.error('You must specify test email recipient using -t/--to parameter') + sys.exit(1) + + email_client.templates = dict( + test=dict( + 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}.") + ), + html=( + "Just a test email. (sent at {sent_date})" if not options.test_mako else + MakoTemplate("Just a test email. (sent at ${sent_date})") + ) + ) + ) + + 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) diff --git a/setup.py b/setup.py index e225aae..ecf59b1 100644 --- a/setup.py +++ b/setup.py @@ -61,6 +61,7 @@ setup( entry_points={ 'console_scripts': [ 'mylib-test-email = mylib.scripts.email_test:main', + 'mylib-test-email-with-config = mylib.scripts.email_test_with_config:main', 'mylib-test-pbar = mylib.scripts.pbar_test:main', 'mylib-test-report = mylib.scripts.report_test:main', 'mylib-test-ldap = mylib.scripts.ldap_test:main',