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.
email_log.txt
), helping track the process.leads.csv
) with the lead information (name, email, company, industry, etc.).import pandas as pd
import smtplib
import os
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import logging
logging.basicConfig(filename='email_log.txt', [level=logging.INFO](<http://level=logging.info/>))
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()