Skip to content

AskewCow/email-automation

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“§ Email Automation Tool

A powerful, flexible Python script for sending personalized HTML emails with scheduling capabilities. Perfect for academic research, surveys, marketing campaigns, and professional outreach.

✨ Features

  • 🎨 Personalized HTML Emails - Use templates with dynamic placeholders
  • πŸ“… Smart Scheduling - Send emails at specific times or dates
  • πŸ§ͺ Safe Testing - Dry run mode to test without sending
  • πŸ“ Comprehensive Logging - Console output with colors + optional file logging
  • πŸ”’ Secure Configuration - Environment variables for sensitive data
  • πŸ“€ BCC Support - Add blind carbon copy recipients
  • πŸ”„ Batch Processing - Handle multiple recipient groups efficiently
  • πŸ›‘οΈ Error Handling - Robust error handling with clear messages

πŸš€ Quick Start

1. Clone & Setup

git clone https://github.com/YourUsername/email-automation.git
cd email-automation
pip install -r requirements.txt

2. Configure Environment

Copy the example configuration:

cp .env.example .env

Edit .env with your details:

# Email Configuration
YOUR_EMAIL=your_email@gmail.com
YOUR_PASSWORD=your_app_password_here
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=465

# Email Content
SUBJECT_LINE=🌟 Your Custom Subject Line
SENDER_NAME=Your Name
AFFILIATION=Your Organization
BCC_EMAILS=supervisor@example.com,backup@example.com

# Safety & Logging
DRY_RUN=true
LOG_TO_FILE=true

# Scheduling (optional)
SEND_AT=14:30
# SEND_AT=2025-07-22 09:00

3. Prepare Recipients

Create email_recipients.json:

[
    {
        "university": "Trinity College Dublin",
        "emails": ["contact@tcd.ie", "admin@tcd.ie"]
    },
    {
        "university": "University College Dublin",
        "emails": ["info@ucd.ie"]
    }
]

4. Test & Send

# Test first (DRY_RUN=true)
python send_emails.py

# Send for real (DRY_RUN=false)
python send_emails.py

πŸ“‹ Detailed Setup Guide

πŸ” Email Authentication Setup

Gmail with 2FA (Recommended)

  1. Enable 2-Factor Authentication in your Google Account
  2. Generate App Password:
    • Go to Google Account Security
    • Navigate: Security β†’ 2-Step Verification β†’ App passwords
    • Select "Mail" and generate password
    • Use the 16-character password in your .env file

Other Email Providers

Provider SMTP Server Port Authentication
Gmail smtp.gmail.com 465 App Password (if 2FA)
Outlook smtp.live.com 587 App Password
Yahoo smtp.mail.yahoo.com 465 App Password
Custom Your server Varies Regular/App Password

πŸ“ File Structure

email-automation/
β”œβ”€β”€ send_emails.py          # Main script
β”œβ”€β”€ email_template.html     # Your HTML template
β”œβ”€β”€ email_recipients.json   # Recipient data
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ .env                   # Your configuration (create this)
β”œβ”€β”€ .env.example          # Configuration template
β”œβ”€β”€ log.txt               # Generated log file (optional)
└── README.md            # This file

🎨 HTML Template Format

Your email_template.html should include these placeholders:

<!DOCTYPE html>
<html>
    <body>
        <h2>Dear {{Recipient Department/Office}},</h2>

        <p>Your email content here...</p>

        <p>
            Sincerely,<br />
            {{Your Name/Project Name}}<br />
            {{Your Affiliation/Study Area}}
        </p>
    </body>
</html>

Placeholders:

  • {{Recipient Department/Office}} β†’ Replaced with university from JSON
  • {{Your Name/Project Name}} β†’ Replaced with SENDER_NAME from .env
  • {{Your Affiliation/Study Area}} β†’ Replaced with AFFILIATION from .env

βš™οΈ Configuration Options

πŸ“§ Email Settings

YOUR_EMAIL=your_email@gmail.com          # Your sending email
YOUR_PASSWORD=app_password_here          # App password (not regular password!)
SMTP_SERVER=smtp.gmail.com              # Email provider's SMTP server
SMTP_PORT=465                           # SMTP port (465 for SSL, 587 for TLS)

πŸ“ Content Settings

SUBJECT_LINE=Your Email Subject          # Email subject line
SENDER_NAME=Your Full Name              # Your name in email signature
AFFILIATION=Your Organization           # Your organization/department
BCC_EMAILS=email1@ex.com,email2@ex.com # Comma-separated BCC recipients

πŸ“‚ File Settings

HTML_TEMPLATE_FILE=email_template.html  # Path to HTML template
JSON_FILE=email_recipients.json         # Path to recipients JSON
LOG_FILE=log.txt                        # Log file path

πŸ§ͺ Testing & Logging

DRY_RUN=true                            # Test mode (true/false)
LOG_TO_FILE=true                        # Save logs to file (true/false)

⏰ Scheduling

SEND_AT=                                # Send immediately (empty)
SEND_AT=14:30                           # Send today at 2:30 PM
SEND_AT=2025-07-22 09:00               # Send on specific date/time

🎯 Usage Examples

Example 1: Immediate Send

# .env settings
DRY_RUN=false
SEND_AT=

Example 2: Schedule for Later Today

# .env settings
SEND_AT=15:30                           # Send at 3:30 PM today

Example 3: Schedule for Future Date

# .env settings
SEND_AT=2025-07-25 09:00               # Send on July 25th at 9 AM

Example 4: Test Campaign

# .env settings
DRY_RUN=true                           # Safe testing mode
LOG_TO_FILE=true                       # Save detailed logs

πŸ” Understanding the Output

The script provides colorized console output:

πŸš€ EMAIL AUTOMATION SCRIPT STARTED
══════════════════════════════════════════════════════════

ℹ️  πŸ“§ Sender: your_email@gmail.com
ℹ️  πŸ“„ Template: email_template.html
ℹ️  πŸ“‚ Recipients: email_recipients.json
⚠️  πŸ§ͺ DRY RUN MODE: No emails will be sent!

πŸ“¨ STARTING EMAIL CAMPAIGN
══════════════════════════════════════════════════════════

🏫 πŸ“ Processing Trinity College Dublin (2 emails)
βœ… βœ‰οΈ  Sent to: contact@tcd.ie
βœ… βœ‰οΈ  Sent to: admin@tcd.ie

πŸ“ˆ CAMPAIGN SUMMARY
══════════════════════════════════════════════════════════
βœ… πŸ“§ Successfully sent: 2
βœ… βœ… Total processed: 2
⚠️  πŸ§ͺ This was a DRY RUN - no actual emails were sent

πŸ›‘οΈ Security Best Practices

βœ… DO:

  • βœ… Use App Passwords for 2FA-enabled accounts
  • βœ… Keep .env file out of version control (it's in .gitignore)
  • βœ… Test with DRY_RUN=true first
  • βœ… Use BCC for sensitive recipient lists
  • βœ… Review logs before and after sending

❌ DON'T:

  • ❌ Use your regular email password with 2FA enabled
  • ❌ Commit .env file to Git
  • ❌ Send to large lists without testing first
  • ❌ Include sensitive data in recipient JSON if sharing

πŸ”§ Troubleshooting

"Authentication Failed" Error

  • βœ… Verify you're using an App Password (not regular password)
  • βœ… Check SMTP server and port are correct
  • βœ… Ensure 2FA is enabled and App Password is generated

"File Not Found" Error

  • βœ… Check file paths in .env are correct
  • βœ… Ensure email_template.html and email_recipients.json exist
  • βœ… Verify you're running script from correct directory

"Invalid JSON" Error

  • βœ… Validate JSON syntax using online JSON validator
  • βœ… Check for trailing commas or missing quotes
  • βœ… Ensure UTF-8 encoding

Scheduling Not Working

  • βœ… Use correct format: HH:MM or YYYY-MM-DD HH:MM
  • βœ… Keep terminal/command prompt open during scheduled wait
  • βœ… Ensure computer doesn't sleep during wait time

πŸ“Š Advanced Features

Logging

Enable file logging for detailed records:

LOG_TO_FILE=true
LOG_FILE=campaign_2025_07_21.log

Multiple BCC Recipients

Add multiple supervisors or monitoring emails:

BCC_EMAILS=supervisor@uni.edu,backup@org.com,monitor@dept.edu

Custom File Locations

Organize your files:

HTML_TEMPLATE_FILE=templates/survey_invitation.html
JSON_FILE=data/research_participants.json
LOG_FILE=logs/campaign_$(date).log

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

If you encounter issues:

  1. Check the troubleshooting section above
  2. Enable logging (LOG_TO_FILE=true) for detailed error information
  3. Test in DRY_RUN mode to isolate issues
  4. Open an issue with:
    • Error messages (remove sensitive data)
    • Your configuration (remove credentials)
    • Steps to reproduce

Made with ❀️ for efficient email automation

About

Python Email Automation Script with Scheduling

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 76.8%
  • HTML 23.2%