Created
December 16, 2024 05:00
-
-
Save Abhayparashar31/ecd1b63cfa3e4cba052453e38ec607ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib, ssl, email | |
from email import encoders | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
from email.utils import formataddr | |
import pandas as pd | |
# Sender email credentials | |
sender_email = "YOUR_EMAIL" | |
password = "YOUR_EMAIL_PASSWORD" | |
# Load HTML template | |
with open("template.html", "r") as file: | |
html_template = file.read() | |
# Load recipient data from CSV file | |
recipients_df = pd.read_csv("recipients.csv") # CSV should have 'name' and 'email' columns | |
# Function to send an email to a single recipient | |
def send_email(recipient_name, recipient_email): | |
# Create MIMEMultipart email message | |
msg = MIMEMultipart("alternative") | |
msg["Subject"] = "Welcome Pythoneer!!!" | |
msg["From"] = formataddr(("The Pythoneers", "Publication")) | |
msg["To"] = recipient_email | |
# Customize the HTML content | |
personalized_html = html_template.replace("[User Name]", recipient_name) | |
# Attach personalized HTML content to the email | |
part = MIMEText(personalized_html, "html") | |
msg.attach(part) | |
# Send the email | |
context = ssl.create_default_context() | |
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: | |
server.login(sender_email, password) | |
server.sendmail("The Pythoneers", recipient_email, msg.as_string()) | |
print(f"Email sent to {recipient_name} at {recipient_email}") | |
# Loop through each recipient and send an email | |
for _, row in recipients_df.iterrows(): | |
send_email(row["name"], row["email"]) | |
print("All emails have been sent!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment