0% found this document useful (0 votes)
17 views5 pages

Mail Merge Code

This document outlines a Python script that automates the process of sending emails with attachments to multiple recipients listed in an Excel file. It includes steps for loading the Excel file, entering email credentials, setting up an SMTP connection, and iterating through each row to send emails while updating their status. Finally, it saves the updated status back to the Excel file and closes the email server connection.

Uploaded by

mridhul Pradeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Mail Merge Code

This document outlines a Python script that automates the process of sending emails with attachments to multiple recipients listed in an Excel file. It includes steps for loading the Excel file, entering email credentials, setting up an SMTP connection, and iterating through each row to send emails while updating their status. Finally, it saves the updated status back to the Excel file and closes the email server connection.

Uploaded by

mridhul Pradeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import pandas as pd

import smtplib

import os

from [Link] import EmailMessage

# -------------------- STEP 1: LOAD YOUR EXCEL FILE --------------------

excel_file = 'your_excel_file.xlsx' # 🔁 Change to your actual file name

df = pd.read_excel(excel_file)

# -------------------- STEP 2: ENTER YOUR EMAIL CREDENTIALS --------------------

EMAIL_ADDRESS = 'your_email@[Link]' # 🔁 Replace with your Outlook email

EMAIL_PASSWORD = 'your_password_or_app_pass' # 🔁 Use App Password if 2FA enabled

# -------------------- STEP 3: SET UP SMTP CONNECTION TO OUTLOOK --------------------

server = [Link]('[Link]', 587)

[Link]()

[Link](EMAIL_ADDRESS, EMAIL_PASSWORD)

# -------------------- STEP 4: LOOP THROUGH EACH ROW IN EXCEL --------------------

for index, row in [Link]():

status = str([Link]('Status')).strip().lower()

if status == 'sent':

print(f" Skipping already sent email to {row['Company_Mail_id']}")

continue

to_email = row['Company_Mail_id']

company = row['Company Name']

attachment_path = row['Attachment']
# Create the email

msg = EmailMessage()

msg['Subject'] = f'Application for a role at {company}'

msg['From'] = EMAIL_ADDRESS

msg['To'] = to_email

# Email body

msg.set_content(f'''Dear {company} HR Team,

I am writing to express my interest in a suitable position at your company.

Please find my resume attached.

Thanks & Regards,

Mridhul T P

''')

# Attach the file

if [Link](attachment_path):

with open(attachment_path, 'rb') as f:

file_data = [Link]()

file_name = [Link](attachment_path)

msg.add_attachment(file_data, maintype='application', subtype='octet-stream',


filename=file_name)

else:

print(f"❌ Attachment not found for {company}: {attachment_path}")

continue
# Try sending the email

try:

server.send_message(msg)

print(f"✅ Email sent to {to_email}")

[Link][index, 'Status'] = 'Sent' # Mark as sent

except Exception as e:

print(f"❌ Failed to send to {to_email}: {e}")

# -------------------- STEP 5: SAVE UPDATED EXCEL WITH STATUS --------------------

df.to_excel(excel_file, index=False)

print("📁 Excel updated with 'Sent' status.")

# -------------------- STEP 6: CLOSE THE EMAIL SERVER CONNECTION --------------------

[Link]()

You might also like