Skip to main content

A tool to send many mails from csv and a template

Project description

csvformatmail: A tool to format mails from csv and to send them

Installing

From pypi:

pipx install csvformatmail

You can also use pip if you do not want to install in a contained environnement.

Or developer version:

git clone <this repo>
cd csvformatmail
pip3 install -e .

(Optional) Enable completion (for bash or other shells using bash-completion)

mkdir -p ~/.local/share/bash-completion/completions
register-python-argcomplete csvformatmail > ~/.local/share/bash-completion/completions/csvformatmail

Example

Write a mail template:

From: My name <my-mail-address@example.org>
To: {{mail}}
Bcc: my-mail-address@example.org
Subject: Result of the last test

Dear {{firstname}} {{lastname.capitalize()}},

Your results of the last test are:

 - Part A: {{"{:.1f}".format(a)}}
 - Part B: {{"{:.1f}".format(b)}}

Therefore you {% if (a+b)/2>=10 %}pass{%else%}fail{%endif%} the test.
{% if (a+b)/2<10 %}
Another test session will be organized soon.
{%endif%}
-- 
Your teacher

Use a csv file with (at least) the columns firstname, lastname, mail, a and b:

firstname,lastname,mail,a,b,c
Jacques,MARTIN,jacques.martin@example.org,12.54441,14,1111.221
…

And the command if you use a distant smtp:

csvformatmail -h smtp.example.org -l mylogin template.txt -t a:float -t b:float listing.csv

Or if you have a local smtp:

csvformatmail template.txt -t a:float -t b:float listing.csv

Attachments

The template file can contain the header "Attachments" to specify attachments. The value of this header is a comma separated list of paths to files to attach. The files must be accessible from the current working directory.

Example:

From: My name <my-mail-address@example.org>
To: {{mail}}
Bcc: my-mail-address@example.org
Subject: Result of the last test
Attachments: /path/to/file1.txt, {{filename}}.pdf

Dear {{firstname}} {{lastname.capitalize()}},

Your results in attachments.

Here, the csv file must contains columns filename, mail, firstname, and lastname.

Example with formatting and conditional rendering

The template file can contain python definition, and all the rows for each column is accessible by the names allrows (default name, can be changed with arg --allrows-name). See the following template file:

From: My name <my-mail-address@example.org>
To: {{mail}}
Bcc: my-mail-address@example.org
Subject: Result of the last test

Dear {{firstname}} {{lastname.capitalize()}},

Your results of the last test are:

 - Part A: {{"{:.1f}".format(a)}}
 - Part B: {{"{:.1f}".format(b)}}

For you information, the statistics for part A are:
 - mean: {{'{:.2f}'.format(np.mean(allrows['a']))}}
 - standard deviation: {{'{:.2f}'.format(np.std(allrows['a']))}}.

Therefore you {% if (a+b)/2>=10 %}pass{%else%}fail{%endif%} the test.
{% if (a+b)/2<10 %}
Another test session will be organized soon.
{%endif%}
-- 
Your teacher.

And the command if you use a distant smtp:

csvformatmail -h smtp.example.org -l mylogin template.txt --np -t a:float -t b:float listing.csv

S/MIME Signing

The tool supports S/MIME signing of emails using either gpgsm or a PKCS#12 certificate.

Using gpgsm (recommended)

If you have gpgsm installed and configured (usually part of gnupg2 package), you can sign emails using:

# Automatic signing using the key matching the From address
csvformatmail template.txt -s data.csv

# Or explicitly use gpgsm
csvformatmail template.txt --sign-gpgsm data.csv

The signing key will be automatically selected based on the From address in your email template.

Using PKCS#12 Certificate

If you don't have gpgsm or prefer using a PKCS#12 certificate:

csvformatmail template.txt --sign-p12 certificate.p12 data.csv

The password for the PKCS#12 file will be prompted interactively.

Misc

Consider use function fill and indent of textwrap module. For this you need use -b "import textwrap" or add a python preamble in you template file. e.g. in a template to rewrap to 72 chars:

{{textwrap.fill(somevalue, width=72)}}

Or, for rewrap and indent by > :

{{textwrap.indent(textwrap.fill(somevalue, width=70), "> ")}}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

csvformatmail-1.3.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

csvformatmail-1.3.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file csvformatmail-1.3.0.tar.gz.

File metadata

  • Download URL: csvformatmail-1.3.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for csvformatmail-1.3.0.tar.gz
Algorithm Hash digest
SHA256 6f1025837c36ebf3f36d51e6b04561618ae4458137d78770d2e8bc2883f6af18
MD5 84b0669735f14eca1f78f2533bfc4cc1
BLAKE2b-256 bd959efa2609fde8f623f44ed9373cf469af066fac871497ed25e904c34a4f59

See more details on using hashes here.

File details

Details for the file csvformatmail-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: csvformatmail-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for csvformatmail-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b449cea77b73f66748a65d1b61dcf971fbf4b5404a458071c8c77fc32db7aa9f
MD5 692cb69c864916cd434143719605f986
BLAKE2b-256 866dfb210a23ca57dd8cddb1727ad0ccc9f920ecfd3cd74c80fe39be2675443d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page