Automating some basic Gmail tasks with Python + SMTP
- Python 3.x
- Pip (Python package installer)
- Virtual Env (Recommended)
python -m venv <env-name>
pip install requirements.txt
- Create a project in the Google Cloud Console.
- Following the Quickstart Guide, enable the Gmail API and create OAuth credentials (OAuth client ID) and download the JSON file.
Note: Check Gmail API Usage Limits first.
Create data directory, using the test directory as a template
├── data
│ ├── email_message.txt -> email message body
| ├── emails.csv -> emails + fields to replace in email message
Create .env
file in project directory, add subject and sender email address as environment variables.
SUBJECT="This is my email subject"
EMAIL="sender@gmail.com"
Run script to send an email to each name/email row in emails.csv
.
- All fields enclosed with
{{field name}}
inemail_message.txt
must correspond to a column header inemails.csv
, with values to replace{{field name}}
in the actual email message bodies. This can be used to customize names, specific words in individual emails. -d
tag specifies name of data directory described above. Defaults totest/
directory.
python bulk_email_sender.py -d .data
emails.csv
:
Email,Name,Project Topics
test@gmail.com,Alice,The Forever Waterbottle
Email will be formatted as shown:
From: <My Email>
Subject: New Project with <Name 1>
To: <Email 1>
Hello {{Name}},
This is a test email! Would you be interested in our new product {{Product Name}}