magpie.cli.send_email

Sends email notification using SMTP connection defined from configuration.

Useful for validation of SMTP settings retrieved from an INI file or debugging the rendered email contents.

Attributes

DEFAULT_TEMPLATE_MAPPING

UserConfig

LOGGER

Classes

EmailTemplateChoiceFormatter

Formatter for generating usage messages and argument help strings.

Functions

get_email_template(→ mako.template.Template)

Retrieves the template file with email content matching the custom application setting or the corresponding default.

send_email(→ bool)

Send email notification using provided template and parameters.

make_logging_options(→ None)

Defines argument parser options for logging operations.

setup_logger_from_options(→ None)

Uses argument parser options to setup logging level from specified flags.

get_logger(→ logging.Logger)

Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when

get_settings_from_config_ini(...)

make_parser(→ argparse.ArgumentParser)

main(→ Any)

Module Contents

magpie.cli.send_email.DEFAULT_TEMPLATE_MAPPING[source]
magpie.cli.send_email.get_email_template(template_constant: magpie.typedefs.Str, container: magpie.typedefs.AnySettingsContainer | None = None) mako.template.Template[source]

Retrieves the template file with email content matching the custom application setting or the corresponding default.

Allowed values of template_constant are:

Raises:

IOError – if an explicit override value of the requested template cannot be located.

Returns:

template formatter from the requested template file.

magpie.cli.send_email.send_email(recipient: magpie.typedefs.Str, container: magpie.typedefs.AnySettingsContainer, template: mako.template.Template, parameters: TemplateParameters | None = None) bool[source]

Send email notification using provided template and parameters.

The preparation steps of the email (retrieve SMTP configuration, setup the SMTP connection, define email content parameters and attempt template generation) will directly raise if invalid as they correspond to incorrect application code or configuration settings.

Following step to send the email with the established SMTP connection is caught and logged if raising an exception. This is to allow the calling operation to ignore failing email notification and act accordingly using the resulting email status.

Parameters:
  • recipient – Email address of the intended recipient to which the email must be sent.

  • template – Mako template used for the email contents.

  • container – Any container to retrieve application settings.

  • parameters – Parameters to provide for templating email contents. They are applied on top of various defaults values provided to all emails.

Raises:

any SMTP server configuration, template generator or parameter parsing error for email setup.

Returns:

success status of the notification email (sent without error, no guarantee of reception).

magpie.cli.send_email.make_logging_options(parser: argparse.ArgumentParser) None[source]

Defines argument parser options for logging operations.

magpie.cli.send_email.setup_logger_from_options(logger: logging.Logger, args: argparse.Namespace) None[source]

Uses argument parser options to setup logging level from specified flags.

Setup both the specific CLI logger that is provided and the generic magpie logger.

magpie.cli.send_email.get_logger(name: magpie.typedefs.Str, level: int | None = None, force_stdout: bool = None, message_format: magpie.typedefs.Str | None = None, datetime_format: magpie.typedefs.Str | None = None) logging.Logger[source]

Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when level is logging.NOTSET.

magpie.cli.send_email.get_settings_from_config_ini(config_ini_path: magpie.typedefs.Str, ini_main_section_name: magpie.typedefs.Str = 'app:magpie_app') magpie.typedefs.SettingsType[source]
magpie.cli.send_email.UserConfig[source]
magpie.cli.send_email.LOGGER[source]
class magpie.cli.send_email.EmailTemplateChoiceFormatter(prog, indent_increment=2, max_help_position=24, width=None)[source]

Bases: argparse.HelpFormatter

Formatter for generating usage messages and argument help strings.

Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail.

_format_action(action)[source]

Override the returned help message with available options and shortcuts for email template selection.

magpie.cli.send_email.make_parser() argparse.ArgumentParser[source]
magpie.cli.send_email.main(args: Sequence[magpie.typedefs.Str] | None = None, parser: argparse.ArgumentParser | None = None, namespace: argparse.Namespace | None = None) Any[source]