-1

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)
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
John lewis
  • 67
  • 7
  • [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – ThePyGuy Mar 29 '21 at 15:59
  • I suggest you take a look at [MRE](https://stackoverflow.com/help/minimal-reproducible-example). – miquelvir Mar 29 '21 at 15:59

1 Answers1

0

You can use zip:

names = ["John", "Mike"]
emails = ["John@work.com", "Mike@work.com"]
count = 0
for email, name in zip(emails, names):
    print(f"""Hi {name}, I hope you received this e-mail with the attachment""")

    count += 1
    print("E-mail Sent to %s: " % email, count)
miquelvir
  • 1,748
  • 1
  • 7
  • 21
  • With the zip the following output is showing like this: Hi J,o,h,n, ,S,m,i,t,h, I hope you received this e-mail. It seems to be separating the name. – John lewis Mar 29 '21 at 16:04
  • In my example it works fine. What is the value of `names = email_list['Name']`? – miquelvir Mar 29 '21 at 16:08
  • Sorry for my ignorance but what do you mean by what is the value of names = email_list['Name']? If I understand you correctly, inside names there is the list of names inside my spreadsheet. I think my issue is that I didn't initialize the variables like you did since I'm getting the value from the spreadsheet itself. – John lewis Mar 29 '21 at 16:11
  • That's what I am trying to find out, because it seems to be a string. Can you print the value and type? – miquelvir Mar 29 '21 at 16:11
  • 0 John Smith 1 David Wilson Name: Name, dtype: object Seems to be that the data type is an object? Does it mean I have to make it a string? Thanks for your assistance once again. – John lewis Mar 29 '21 at 16:14
  • I think you did not remove `", ".join(names)` for `name`. – miquelvir Mar 29 '21 at 16:28
  • You were absolutely right Sir! Sorry for the inconvenience and thanks a lot for your assistance! – John lewis Mar 29 '21 at 16:40