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.2.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.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: csvformatmail-1.3.2.tar.gz
  • Upload date:
  • Size: 12.4 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.2.tar.gz
Algorithm Hash digest
SHA256 ca83ca5ccf938dc197cf3f9fdc122b013b24b1b00cceb75fb8d9cf92d8a7e87c
MD5 1b4bf9a7e98c4b641100dff9d875cc23
BLAKE2b-256 bc3d2445358c789ab23d9638dea07d5bff9e07354ca85fb0c0eb15203efae17d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: csvformatmail-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b12bc0ab764bdbd8a18ecf91f3d76f563bdd7e9a29b8b76f2d5f69ad1bf088d4
MD5 cc980fa2b31ae13f13ce379a20637fb0
BLAKE2b-256 aae960db02ffc72ed6244fc4b9506ed5c91b06f46bef248c245d805262f71df2

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