Home > Software engineering >  How to send emails for each person in a excel file
How to send emails for each person in a excel file

Time:07-15

So i have a file em.xlsx where i have Name & Email columns, i want to send email when Name matchs the the filename in a directory

How can i do that ? so far i have this code below, but it actualy return nothing

import glob
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
from collections import ChainMap
# Spreadsheet with emails and  names
email_list = pd.read_excel(r'C:\Users\arabw\OneDrive\Bureau\html\em.xlsx')
folder_path= "C:/Users/arabw/OneDrive/Bureau/html/"
my_files=[{each_file.split(".")[0]:each_file} for each_file in os.listdir(folder_path) if each_file.endswith(".csv")]
my_files_dict = dict(ChainMap(*my_files))
# getting the names and the emails
names = email_list['Name']
emails = email_list['Email']
 
for i in range(len(emails)): # iterate through the records
    # for every record get the name and the email addresses
    name = names[i]
    email = emails[i]
    if my_files_dict.get(name):
        print(f"file found:{my_files_dict.get(name)}") # attach this file : my_files_dict.get(name)
    #Some  help needed from here I believe
    while name == os.path:
         smtp_ssl_host = 'xxxxx'
         smtp_ssl_port = 465
         email_from = "xxxxx"
         email_pass = "xxxxx"
         email_to = email
         msg2 = MIMEMultipart()
         msg2['Subject'] = "Present Record(s)"
         msg2['From'] = email_from
         msg2['To'] = email
         fo=open(my_files_dict.get(name),'rb')
         attach = email.mime.application.MIMEApplication(fo.read(),_subtype="xlsx")
         fo.close()
         attach.add_header('Content-Disposition','attachment',my_files_dict.get(name))
         msg.attach(attach)
         s2 = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
         s2.login(email_from, email_pass)  
         s2.send_message(msg)
         s2.quit()

CodePudding user response:

Following code passed my test. Hope it can help you. I'm not sure what's while name == os.path: for, so I just ignore it.

import glob
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import email
import os
from collections import ChainMap
# Spreadsheet with emails and  names
email_list = pd.read_excel('email.xlsx')
folder_path= './Files/A'
my_files=[{each_file.split(".")[0]:each_file} for each_file in os.listdir(folder_path) if each_file.endswith(".csv")]
my_files_dict = dict(ChainMap(*my_files))
# getting the names and the emails
names = email_list['Name']
emails = email_list['Email']

for i in range(len(emails)): # iterate through the records
    # for every record get the name and the email addresses
    name = names[i]
    email_item = emails[i]
    file_name = my_files_dict.get(name)
    if file_name:
        print(f"file found:{file_name}") # attach this file : my_files_dict.get(name)
    #Some  help needed from here I believe
        # while name == os.path:
        smtp_ssl_host = 'smtp.xxx.com'
        smtp_ssl_port = 465
        email_from = "xxxxx"
        email_pass = "xxxxx"
        email_to = email_item
        msg = MIMEMultipart()
        msg['Subject'] = "Present Record(s)"
        msg['From'] = email_from
        msg['To'] = email_item
        fo=open(os.path.join(folder_path, file_name), 'rb')
        attach = MIMEApplication(fo.read(), _subtype="xlsx")
        fo.close()
        attach.add_header('Content-Disposition', 'attachment', filename=file_name)
        msg.attach(attach)
        smtp = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
        try:
            smtp.login(email_from, email_pass)
            smtp.sendmail(email_from, email_to, msg.as_string())
        except smtplib.SMTPException as e:
            print("send fail", e)
        else:
            print("success")
        finally:
            try:
                smtp.quit()
            except smtplib.SMTPException:
                print("quit fail")
            else:
                print("quit success")
  • Related