A Python script to send personalized emails with attachments to multiple recipients using Gmail SMTP and Google App Password.
- Send emails with personalized bodies and attachments to a list of contacts from a CSV file.
- Securely load configuration from environment variables via a
.envfile. - Use Gmail SMTP with TLS for secure email delivery.
- Python 3.6 or higher
- A Gmail account with 2-Step Verification enabled
- A generated Google App Password for SMTP authentication
- Clone this repository:
git clone <repository-url> cd <repository-directory>
- (Optional) Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows use: venv\Scripts\activate
- Install required dependencies:
pip install -r requirements.txt
Create a .env file in the root of the project with the following variables:
SENDER_EMAIL=your-email@gmail.com # Your Gmail address
APP_PASSWORD=your-app-password # Your 16-character Google App Password
EMAIL_SUBJECT=Your email subject # Subject line for the sent emails
CSV_FILE_PATH=path/to/contacts.csv # Path to your CSV file containing contacts- Sign in to your Google Account and navigate to Security: https://myaccount.google.com/security
- Under "Signing in to Google", ensure 2-Step Verification is enabled.
- Click on App passwords.
- In the Select app dropdown, choose Mail, then select your device or Other (Custom name), e.g., "Email Script".
- Click Generate.
- Copy the 16-character app password and place it in your
.envfile as the value forAPP_PASSWORD.
Run the script to send emails to all contacts listed in the CSV file:
python send_emails.pyThe CSV file should have the following format (with a header row):
name,email,attachment_path
John Doe,john.doe@example.com,/path/to/file1.pdf
Jane Smith,jane.smith@example.com,/path/to/file2.pdf
- FileNotFoundError: Ensure the paths in
CSV_FILE_PATHand eachattachment_pathare correct. - SMTPAuthenticationError: Verify your
SENDER_EMAILandAPP_PASSWORD. Make sure your App Password is active and 2-Step Verification is set up. - Other Exceptions: Review the console output for detailed error messages.
This project is licensed under the MIT License. Feel free to modify and distribute.