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
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), use the -E option:
csvformatmail -E iso8859-15 template.txt contacts.csv
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.
Other server options are available, see csvformatmail --help.
Attachments
Add attachments using the Attachments 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)}}
Accessing All Rows
Use allrows in the templateto 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file csvformatmail-1.3.1.tar.gz.
File metadata
- Download URL: csvformatmail-1.3.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e8ee09eb55aa3f31353bad60e68f2b13d94d16f55a2642ed2ec1298b844bfad
|
|
| MD5 |
66454d14d3718d4bcf59e9e2ccb82f35
|
|
| BLAKE2b-256 |
56d8eae030398e2cd8ebe6aff5d9e26ce8929ad0bdd00997983a55453caacb78
|
File details
Details for the file csvformatmail-1.3.1-py3-none-any.whl.
File metadata
- Download URL: csvformatmail-1.3.1-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
260235d512816cd9422809f90bcd089c79e3b048423b501f69d0edee4f5d24a5
|
|
| MD5 |
4452d11055dbe9e4ef00b3c1e4462864
|
|
| BLAKE2b-256 |
a7f5e1a8740355e74641e5e4de52767483b4274659f90bc4cfbca8346ea7ddda
|