2021-05-19 18:07:42 +02:00
|
|
|
""" Test Email client """
|
|
|
|
import datetime
|
2023-01-16 12:56:12 +01:00
|
|
|
import getpass
|
2021-05-19 18:07:42 +02:00
|
|
|
import logging
|
2023-03-13 18:58:20 +01:00
|
|
|
import os
|
2021-05-19 18:07:42 +02:00
|
|
|
import sys
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
from mylib.scripts.helpers import add_email_opts, get_opts_parser, init_email_client, init_logging
|
2021-05-19 18:07:42 +02:00
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
log = logging.getLogger("mylib.scripts.email_test")
|
2021-05-19 18:07:42 +02:00
|
|
|
|
2021-05-19 19:19:57 +02:00
|
|
|
|
|
|
|
def main(argv=None): # pylint: disable=too-many-locals,too-many-statements
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Script main"""
|
2021-05-19 18:07:42 +02:00
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv[1:]
|
|
|
|
|
|
|
|
# Options parser
|
|
|
|
parser = get_opts_parser(just_try=True)
|
2023-03-13 18:58:20 +01:00
|
|
|
add_email_opts(
|
|
|
|
parser,
|
|
|
|
templates_path=os.path.join(os.path.dirname(os.path.realpath(__file__)), "email_templates"),
|
|
|
|
)
|
2021-05-19 18:07:42 +02:00
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
test_opts = parser.add_argument_group("Test email options")
|
2021-05-19 18:07:42 +02:00
|
|
|
|
|
|
|
test_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-t",
|
|
|
|
"--to",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="test_to",
|
2023-03-14 16:04:53 +01:00
|
|
|
help="Test email recipient(s)",
|
|
|
|
nargs="+",
|
2021-05-19 18:07:42 +02:00
|
|
|
)
|
|
|
|
|
2023-03-13 18:58:20 +01:00
|
|
|
test_opts.add_argument(
|
|
|
|
"-T",
|
|
|
|
"--template",
|
|
|
|
action="store_true",
|
|
|
|
dest="template",
|
|
|
|
help="Template name to send (default: test)",
|
|
|
|
default="test",
|
|
|
|
)
|
|
|
|
|
2021-05-19 18:07:42 +02:00
|
|
|
test_opts.add_argument(
|
2023-01-16 12:56:12 +01:00
|
|
|
"-m",
|
|
|
|
"--mako",
|
2021-05-19 18:07:42 +02:00
|
|
|
action="store_true",
|
|
|
|
dest="test_mako",
|
|
|
|
help="Test mako templating",
|
|
|
|
)
|
|
|
|
|
2023-03-14 16:36:26 +01:00
|
|
|
test_opts.add_argument(
|
|
|
|
"--cc",
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="test_cc",
|
|
|
|
help="Test CC email recipient(s)",
|
|
|
|
nargs="+",
|
|
|
|
)
|
|
|
|
|
|
|
|
test_opts.add_argument(
|
|
|
|
"--bcc",
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="test_bcc",
|
|
|
|
help="Test BCC email recipient(s)",
|
|
|
|
nargs="+",
|
|
|
|
)
|
|
|
|
|
2021-05-19 18:07:42 +02:00
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
if not options.test_to:
|
2023-03-14 16:04:53 +01:00
|
|
|
parser.error("You must specify at least one test email recipient using -t/--to parameter")
|
2021-05-19 18:07:42 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Initialize logs
|
2023-01-16 12:56:12 +01:00
|
|
|
init_logging(options, "Test EmailClient")
|
2021-05-19 18:07:42 +02:00
|
|
|
|
|
|
|
if options.email_smtp_user and not options.email_smtp_password:
|
2023-01-16 12:56:12 +01:00
|
|
|
options.email_smtp_password = getpass.getpass("Please enter SMTP password: ")
|
2021-05-19 18:07:42 +02:00
|
|
|
|
2023-03-13 18:58:20 +01:00
|
|
|
email_client = init_email_client(options)
|
2021-05-19 18:07:42 +02:00
|
|
|
|
2023-03-14 16:36:26 +01:00
|
|
|
log.info(
|
|
|
|
"Send a test email to %s (CC: %s / BCC: %s)",
|
|
|
|
", ".join(options.test_to),
|
|
|
|
", ".join(options.test_cc) if options.test_cc else None,
|
|
|
|
", ".join(options.test_bcc) if options.test_bcc else None,
|
|
|
|
)
|
|
|
|
if email_client.send(
|
|
|
|
options.test_to,
|
|
|
|
cc=options.test_cc,
|
|
|
|
bcc=options.test_bcc,
|
|
|
|
template="test",
|
|
|
|
sent_date=datetime.datetime.now(),
|
|
|
|
):
|
2023-01-16 12:56:12 +01:00
|
|
|
log.info("Test email sent")
|
2021-05-19 18:07:42 +02:00
|
|
|
sys.exit(0)
|
2023-01-16 12:56:12 +01:00
|
|
|
log.error("Fail to send test email")
|
2021-05-19 18:07:42 +02:00
|
|
|
sys.exit(1)
|