My problem is that when I'm sending an e-mail it should give different names according to the e-mail address and fields stored in the excel spreadsheet. Let's say I only have two recipients in my spreadsheet. How would I get the following expected output? Thanks
Output now: Hi, John Smith David Wilson, I hope you received this e-mail with the attachment.
Hi, John Smith David Wilson, I hope you received this e-mail with the attachment.
Expected Output: Hi, John Smith, I hope you received this e-mail with the attachment.
Hi, David Wilson, I hope you received this e-mail with the attachment.
from email.message import EmailMessage
import pandas as pd
sender_email = input("Enter your e-mail address: ")
password = input("Enter your password: ")
email_list = pd.read_excel("D:\Learning Python\Mini_Projects\emailaddresses.xlsx", engine="openpyxl")
names = email_list['Name']
emails = email_list['Email']
count = 0
for email in emails:
msg = EmailMessage()
msg['Subject'] = "Piano Concert Coming Soon by Jabob!"
msg['From'] = sender_email
msg['To'] = email
msg.set_content(f"""\
Hi {", ".join(names)}, I hope you received this e-mail with the attachment""")
with open('TrainingProgram.pdf', 'rb') as f:
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(sender_email, password)
smtp.send_message(msg)
count += 1
print("E-mail Sent: ", count)