2022-01-19 17:31:40 +01:00
|
|
|
""" 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
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Script main"""
|
2022-01-19 17:31:40 +01:00
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv[1:]
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
config = Config(__doc__, __name__.replace(".", "_"))
|
2022-01-19 17:31:40 +01:00
|
|
|
|
|
|
|
email_client = EmailClient(config=config)
|
|
|
|
email_client.configure()
|
|
|
|
|
|
|
|
# Options parser
|
|
|
|
parser = config.get_arguments_parser(description=__doc__)
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
test_opts = parser.add_argument_group("Test email options")
|
2022-01-19 17:31:40 +01:00
|
|
|
|
|
|
|
test_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-t",
|
|
|
|
"--to",
|
2022-01-19 17:31:40 +01:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="test_to",
|
|
|
|
help="Test email recipient",
|
|
|
|
)
|
|
|
|
|
|
|
|
test_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-m",
|
|
|
|
"--mako",
|
2022-01-19 17:31:40 +01:00
|
|
|
action="store_true",
|
|
|
|
dest="test_mako",
|
|
|
|
help="Test mako templating",
|
|
|
|
)
|
|
|
|
|
|
|
|
options = config.parse_arguments_options()
|
|
|
|
|
|
|
|
if not options.test_to:
|
2023-01-16 12:56:12 +01:00
|
|
|
parser.error("You must specify test email recipient using -t/--to parameter")
|
2022-01-19 17:31:40 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
email_client.templates = dict(
|
|
|
|
test=dict(
|
|
|
|
subject="Test email",
|
|
|
|
text=(
|
2023-01-16 12:56:12 +01:00
|
|
|
"Just a test email sent at {sent_date}."
|
|
|
|
if not options.test_mako
|
|
|
|
else MakoTemplate("Just a test email sent at ${sent_date}.")
|
2022-01-19 17:31:40 +01:00
|
|
|
),
|
|
|
|
html=(
|
2023-01-16 12:56:12 +01:00
|
|
|
"<strong>Just a test email.</strong> <small>(sent at {sent_date})</small>"
|
|
|
|
if not options.test_mako
|
|
|
|
else MakoTemplate(
|
|
|
|
"<strong>Just a test email.</strong> <small>(sent at ${sent_date})</small>"
|
|
|
|
)
|
|
|
|
),
|
2022-01-19 17:31:40 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
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")
|
2022-01-19 17:31:40 +01:00
|
|
|
sys.exit(0)
|
2023-01-16 12:56:12 +01:00
|
|
|
logging.error("Fail to send test email")
|
2022-01-19 17:31:40 +01:00
|
|
|
sys.exit(1)
|