πŸ“§ How to Use the Email Automation Script

This Python script automates sending personalized emails to leads based on their industry (Technology πŸ’» or Finance πŸ’Ό). It customizes the subject and body of each email and sends them out using Gmail's SMTP server.

Key Features:

How It Works:

  1. CSV Input πŸ“: The script loads a CSV file (leads.csv) with the lead information (name, email, company, industry, etc.).
  2. Segmentation 🎯: Leads are split into two groups: Technology πŸ’» and Finance πŸ’Ό.
  3. Sending Emails πŸ“€: For each lead, an email is sent with a personalized subject and body.
  4. Logging and Monitoring 🧾: The script logs the email sends and any errors for easy tracking.
import pandas as pd
import smtplib
import os
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import logging

Set up logging

logging.basicConfig(filename='email_log.txt', [level=logging.INFO](<http://level=logging.info/>))

Function to send emails

def send_email(recipient, subject, body):
sender_email = os.getenv("SENDER_EMAIL")  # Use environment variable for security
password = os.getenv("EMAIL_PASSWORD")  # Use environment variable for security
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = recipient
msg['Subject'] = subject

msg.attach(MIMEText(body, 'plain'))

try:
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(sender_email, password)
    text = msg.as_string()
    server.sendmail(sender_email, recipient, text)
    logging.info(f"Email sent to {recipient}")
    print(f"Email sent to {recipient}")
except Exception as e:
    logging.error(f"Error sending email to {recipient}: {e}")
    print(f"Error: {e}")
finally:
    server.quit()