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

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

  1. Create a template (template.txt):
From: Me <sender@example.com>
To: {{email}}
Subject: Hello {{name}}

Hi {{name}},
Welcome!

Best regards,
-- 
Me
  1. Prepare your data (contacts.csv):
name,email
Alice,alice@example.com
Bob,bob@example.com
  1. 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 -e to 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.

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.3.tar.gz (12.4 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.3-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for csvformatmail-1.3.3.tar.gz
Algorithm Hash digest
SHA256 4700ffe81ee87442acc528d6b8e09a2129e07f7a79b2c5b410c1432f0fe8848a
MD5 e97041337bc65ab32e810a8d7c22b0fc
BLAKE2b-256 5c551449395ee2491dfc00b7dabe8d40e02a25ca39f471650aea414b82f58cbd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for csvformatmail-1.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d49f071f77aa294afadcf5f0c98ccaa48bf96b6fd1d2390329c0f8e349096695
MD5 345f4b371572f4d6d16087284c1b6317
BLAKE2b-256 ce661e1f9a4b4f75a7309067c18eebc30f676eb8208db4dcfc227681fd7e0a57

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