Metadata-Version: 2.4
Name: flexible_batch_email_sender
Version: 0.1.2
Summary: A flexible batch email sender for a variety of scripting tasks.
Author-email: Žan Šadl Ferš <zan.sf@disroot.org>
Maintainer-email: Žan Šadl Ferš <zan.sf@disroot.org>
License-Expression: MIT
Project-URL: homepage, https://codeberg.org/ZanS-F/flexible_batch_email_sender
Project-URL: source, https://codeberg.org/ZanS-F/flexible_batch_email_sender.git
Project-URL: issues, https://codeberg.org/ZanS-F/flexible_batch_email_sender/issues
Project-URL: changelog, https://codeberg.org/ZanS-F/flexible_batch_email_sender/src/branch/master/CHANGELOG.md
Keywords: automated,csv,excel,scipting,automation,email,email client,batched
Classifier: Development Status :: 4 - Beta
Classifier: Topic :: Utilities
Classifier: Topic :: Communications :: Email :: Email Clients (MUA)
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: Microsoft :: Windows :: Windows 8
Classifier: Operating System :: Microsoft :: Windows :: Windows 8.1
Classifier: Operating System :: Microsoft :: Windows :: Windows 10
Classifier: Operating System :: Microsoft :: Windows :: Windows 11
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: ConfigArgParse>=1.7.5
Requires-Dist: tqdm>=4.70.0
Requires-Dist: openpyxl>=3.1.5
Requires-Dist: mako>=1.3.12
Provides-Extra: dev
Requires-Dist: build; extra == "dev"
Requires-Dist: pyinstaller; extra == "dev"
Dynamic: license-file

Flexible Batch Email Sender
===========================

A email client tool for sending batches of emails over SMTP as part of a automation script or pipeline.

The email body can be regular text files or Mako templates that get rendered before being send off,
allowing for dynamic content.

CLI interface
-------------

```
usage: flexible_batch_email_sender [-h] [--config-filepath CONFIG_FILEPATH] [--log-filepath LOG_FILEPATH] [--smtp-ssl-host SMTP_SSL_HOST] [--sender-email-address SENDER_EMAIL_ADDRESS]
                                   [--sender-password SENDER_PASSWORD] [--netrc-filepath NETRC_FILEPATH] --data-source-path DATA_SOURCE_PATH [--using-table USING_TABLE] --send-to-column
                                   SEND_TO_COLUMN --subject SUBJECT [--templates-dirpath TEMPLATES_DIRPATH] --message-template-filename MESSAGE_TEMPLATE_FILENAME
                                   [--alternative-message-template-filename ALTERNATIVE_MESSAGE_TEMPLATE_FILENAME] [--attachments-dirpath ATTACHMENTS_DIRPATH]
                                   [--add-attachment ADD_ATTACHMENT] [--match-condition MATCH_CONDITION] [--sending-delay SENDING_DELAY] [--add-template-parameter ADD_TEMPLATE_PARAMETER]
                                   [--show-template-parameters] [--simulate-emails-rendering] [--debug]

A multipurpose program for batch sending templated emails to different recipients.

options:
  -h, --help            show this help message and exit
  --config-filepath CONFIG_FILEPATH
                        If set, will preload the CLI arguments with the provided data from the selected config file. (default: None)
  --log-filepath LOG_FILEPATH
                        If set, will append and store the logs into a file instead of stderr (or basically just displayed over the terminal). (default: None)
  --smtp-ssl-host SMTP_SSL_HOST
                        The hostname of the SMTP server to connect to. Is used also in reading the email user account data from the `.netrc` file. Not required if `--simulate` is set.
                        (default: None)
  --sender-email-address SENDER_EMAIL_ADDRESS
                        The email address of the user account, from which to send the generated emails. If not set, will use what is set in the `netrc` file. (default: None)
  --sender-password SENDER_PASSWORD
                        The password of the user account, from which to send the generated emails. If not set, will use what is set in the `netrc` file. Not required if `--simulate` is
                        set. (default: None)
  --netrc-filepath NETRC_FILEPATH
                        Where the `netrc` file containing the email user accounts login data is stored. If not set, will search for it in the home directory. (default: None)
  --data-source-path DATA_SOURCE_PATH
                        A CSV or Excel file, from which each individual row corresponds to a set of parameters for filling out Mako templates. (default: None)
  --using-table USING_TABLE
                        Excel file only. If defined, will use that specific sheet from that file as the data source. If not, use the first one. (default: None)
  --send-to-column SEND_TO_COLUMN
                        Which column label has the recipients email addresses stored. Used in the row matching to determine to which recipient to send the email. (default: None)
  --subject SUBJECT     The subject of the email. Supports Mako expressions and has access to the parameters provided by the data source. (default: None)
  --templates-dirpath TEMPLATES_DIRPATH
                        Where the email message templates are stored. (default: /home/nitronator/Programming/Projects/Automated Emails/Flexible Batch Email Sender)
  --message-template-filename MESSAGE_TEMPLATE_FILENAME
                        Which textfile template to use from the defined `--templates-dirpath` per matched row. The filename definition supports Mako expressions and has access to the
                        parameters provided by the data source. The template can be any textfile, and support Mako expressions with access to the parameters provided by the data source.
                        (default: None)
  --alternative-message-template-filename ALTERNATIVE_MESSAGE_TEMPLATE_FILENAME
                        Which textfile template to use from the defined `--templates-dirpath` per matched row as a additional content option or fallback. The filename definition supports
                        Mako expressions and has access to the parameters provided by the data source. The template can be any textfile, and support Mako expressions with access to the
                        parameters provided by the data source. (default: None)
  --attachments-dirpath ATTACHMENTS_DIRPATH
                        alternative-message-template-filename From which directory to fetch the attachments to be added for (default: /home/nitronator/Programming/Projects/Automated
                        Emails/Flexible Batch Email Sender)
  --add-attachment ADD_ATTACHMENT
                        Which attachment to add by filename from the defined `--attachments-dirpath` per matched row. The filename definition supports Mako expressions and has access to
                        the parameters provided by the data source. Multiple attachments can be added per matched row (for which using Mako expressions becomes more and more important).
                        (default: None)
  --match-condition MATCH_CONDITION
                        A Python3 expression used to select which rows of the data source to match and use as the templating parameters. Use the `row` or `row_dict` object to access the
                        Excel row attributes, or just `row_dict` if its a CSV file. Use the `extra_params` object to access what was added by `--add-template-parameters` as string values.
                        If a row is matched, a email using its data will be generated and send. If not set, will match no rows and continue till no more rows can be queried. To match all
                        rows, simply use `True`. One thing, the `row` and `row_dict` objects, depending on the data source, could have their values be only strings (CSV), or parsed to the
                        closest interpretable format (Excel). (default: False)
  --sending-delay SENDING_DELAY
                        This is to approximately rate limit how many messages should be send per second to prevent the email server from being overloaded or banning the user account.
                        (default: 2.0)
  --add-template-parameter ADD_TEMPLATE_PARAMETER
                        Adds additional parameters to the template context and `--match-condition` in the form of variable `extra_params`. All values are treated as strings, so if they are
                        (for example) integers, they need to be parsed in the expressions! (default: None)
  --show-template-parameters
                        If set, will disable email sending and only show the set of available template parameters. Very useful if working on new templates and needing to know what can be
                        used in them. (default: False)
  --simulate-emails-rendering
                        If set, will disable email sending and only show the rendered emails in the terminal over stdout, one after another. (default: False)
  --debug               If set, will enable debug handling and logging. (default: False)

By reading the data source and selecting the desired rows by condition matching using Python 3 expressions, each row then corresponds to a email to be send. The matched rows data then act
as Mako template parameters for the email message, with the option to add additional ones over the CLI interface or a config file. To login into the email server, this program requires
credentials to a email user account. If a netrc file is present or defined, its data is taken and used (its data can be overriden by CLI arguments). The expected netrc data is
machine=hostname, login=user email address, password=user account password. The tool supports Mako expressions for templating (see <https://docs.makotemplates.org/en/latest/syntax.html> on
how to write Mako templates).

Args that start with '--' can also be set in a config file (specified via --config-filepath). Uses configparser module to parse an INI file which allows multi-line         values.
Allowed syntax is that for a ConfigParser with the following options:              allow_no_value = False,             inline_comment_prefixes = ("#",)             strict = True
empty_lines_in_values = False          See https://docs.python.org/3/library/configparser.html for details.          Note: INI file sections names are still treated as comments.
In general, command-line values override config file values which override defaults.

```

How to use
----------

The intended way of running this program is **within shell scripts or CI/CD pipelines**,
with different configurations for different purposes.

The CLI arguments can be pre-provided by having a configuration file,
that presets the CLI parameters values when the program runs.
This allows different configurations for different needs to be prepared in a standardized and cross-platform way.

The following is required for use:

* Email account data, from which to send the email batched. Either provided directly over the CLI arguments or
  from a `netrc` file. Set the **appropriate permissions (if ran from a script or using the netrc file)**,
  due to them **containing sensitive information**!
* A data source (`.csv`, `.xlsx` or `.xlsm`), from which the parameters are obtained for filling out the templates.

The program processed *each row of the data source as a potential email to send*.
If *a row is matched*, its data is obtained and processed into template parameters.
One message template is used as the basis for the batch email sending.
If *no templates are used*, then this can be ignored.

Windows specific caveats
------------------------

Due to `argparse` and consequently `ConfigArgParse` using the method `locale.getpreferredencoding` for determining the config file encoding,
it needs to be in the same encoding or transcoded to it.

Another solution (or workaround, to each their own) is to enable [Python UTF-8 mode](https://peps.python.org/pep-0540/) by seting the environment variable `PYTHONUTF8` with the value `1` before running this program, forcing Python v3 to read the config file as a UTF-8 encoded textfile. However, from what i saw, [Python v3.15 and future versions will have it enabled by default](https://pydevtools.com/blog/python-315-utf8-default/). If using the `pyinstaller` produced binaries, it is *enabled by default*.
