csvformatmail: A tool to format mails from csv and to send them
A simple yet powerful tool to send personalized emails using a template and CSV data.
Installation
pipx install csvformatmail # Recommended
# or
pip install csvformatmail
Shell 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
Quick Start
- Create a template (
template.txt):
From: Me <sender@example.com>
To: {{email}}
Subject: Hello {{name}}
Hi {{name}},
Welcome!
Best regards,
--
Me
- Prepare your data (
contacts.csv):
name,email
Alice,alice@example.com
Bob,bob@example.com
- Send emails:
csvformatmail template.txt contacts.csv
Template Files
Templates use the Jinja2 syntax. The structure is:
- Email headers (From, To, Subject are mandatory)
- Empty line
- Email body
Variables from your CSV are accessed using {{variable}}.
If your template file uses a specific encoding (by default the system default encoding is used), see the -E option.
CSV Data
The CSV file provides the data for personalizing each email. Column names in the CSV become variables in the template.
Example:
first_name,last_name,email,order_id
John,Doe,john@example.com,12345
Jane,Smith,jane@example.com,12346
Options:
- Change delimiter:
-d ";"for semicolon-separated files - Use
-eto specify CSV encoding (by default the system default encoding is used). - Rename columns on the fly:
data.csv:email,name=full_name
Sending Emails
Local SMTP Server
csvformatmail template.txt data.csv
Remote SMTP Server with authentication
csvformatmail -h smtp.example.com -l username template.txt data.csv
The password will be prompted interactively if needed. Authentication is always performed in an encrypted tunnel (STARTTLS). Plain text authentication without STARTTLS is deliberately not supported for security reasons.
Other server options are available, see csvformatmail --help.
Attachments
Add attachments using the Attachments fake header:
From: sender@example.com
To: {{email}}
Subject: Your Document
Attachments: common.pdf, {{document}}
Please find attached your documents.
Corresponding CSV file:
email,document
alice@example.com,alice_report.pdf
bob@example.com,bob_report.pdf
The attachment paths can be either fixed or from CSV variables.
Advanced Formatting
Type Conversion and Formatting
Convert CSV columns to specific types for formatting:
csvformatmail -t score:float -t date:str template.txt data.csv
In the template, you can use the formatted value:
Average score: {{"{:.1f}".format(score)}}
Conditional Formatting
Use Jinja2 conditional statements to customize your message:
Your score is {{"{:.1f}".format(score)}}/20.
{% if score >= 10 %}
Congratulations, you passed the test!
{% else %}
Unfortunately, you did not pass the test. You need at least 10/20 to pass.
{% endif %}
Accessing All Rows
Use allrows in the template to access complete columns:
Average score: {{"{:.1f}".format(np.mean(allrows['score']))}}
Enable numpy with --np:
csvformatmail --np template.txt data.csv
Text Formatting
Use textwrap for better text formatting:
csvformatmail -b "import textwrap" template.txt data.csv
Then in template:
{{textwrap.fill(long_text, width=72)}}
{{textwrap.indent(text, "> ")}}
S/MIME Signing
Using gpgsm
# Automatic signing using From address
csvformatmail -s template.txt data.csv
# Explicit gpgsm usage
csvformatmail --sign-gpgsm template.txt data.csv
Using PKCS#12 Certificate
csvformatmail --sign-p12 cert.p12 template.txt data.csv
The certificate password will be prompted interactively.
Metadata
Release files for csvformatmail 1.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| csvformatmail-1.3.3.tar.gz | 12.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| csvformatmail-1.3.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.3 kB
Release files / csvformatmail-1.3.3.tar.gz
| Download URL | csvformatmail-1.3.3.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4700ffe81ee87442acc528d6b8e09a2129e07f7a79b2c5b410c1432f0fe8848a
|
|
BLAKE2b-256 checksum How to use checksums |
5c551449395ee2491dfc00b7dabe8d40e02a25ca39f471650aea414b82f58cbd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.13
|
Release files / csvformatmail-1.3.3-py3-none-any.whl
| Download URL | csvformatmail-1.3.3-py3-none-any.whl |
|---|---|
| Size | 12.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d49f071f77aa294afadcf5f0c98ccaa48bf96b6fd1d2390329c0f8e349096695
|
|
BLAKE2b-256 checksum How to use checksums |
ce661e1f9a4b4f75a7309067c18eebc30f676eb8208db4dcfc227681fd7e0a57
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.13
|