Reputation: 71
I have written a code to send mails with attachment to multiple users using Python. So what this does is, the code will access the csv file, fetch the email address and the corresponding excel file and sends the mail with the attachment.
This code works perfectly fine but the requirement has changed. The new requirement is that, I need to attach multiple files to a single recipient (can't send multiple mails). So for example, I need attach TEST.pdf and another TEST123.pdf file and send to [email protected] (please refer the image).
I have tried my best but I keep on getting an errors in the code. Can someone help me on this.
My Python Code
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import pandas as pd
e = pd.read_csv("C:\\Users\\Hp-Pc\\Desktop\\mail.csv")
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("email", "pw")
body = ("""
Hi There
Please refer attachment
Thanks & Regards
""")
subject = ["PDF"]
fromaddr='email'
for index, row in e.iterrows():
print (row["Emails"]+row["PDF"])
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['Subject'] = row["PDF"]
msg.attach(MIMEText(body, 'plain'))
filename = row["PDF"]
toaddr = row["Emails"]
attachment = open(row["PDF"], "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
print("Emails sent successfully")
server.quit()
Upvotes: 0
Views: 5043
Reputation: 1861
You can use the AutoMailer Application ( made in Python, author : myself )
It can send multiple attachments. It reads recipients from a CSV file
( Don't worry, you can easily export your Excel file to CSV, Google Sheets and Microsoft Excel, and Libre Office all support exporting to CSV format )
https://github.com/aahnik/AutoMailer
This is open-source, You can see how the feature is implemented. Visit the link above
Upvotes: 1
Reputation: 51
Please see below.
from importlib import reload
import win32com.client as win32
import pythoncom
import sys
import os
import pandas as pd
reload(sys)
pythoncom.CoInitialize()
outlook = win32.Dispatch('outlook.application')
def sendmail(receiver, attachment, subject):
receiver = receiver
attachment = attachment
sub = subject
body = "Please find the attached supporting documents for member rebates."
mail = outlook.CreateItem(0)
mail.To = receiver
mail.subject = sub.encode('utf-8').decode('utf-8')
mail.Body = body.encode('utf-8').decode('utf-8')
# Upload multiple attachments/files that meet the conditions/requirements
for i in range(len(attachment)):
mail.Attachments.Add(attachment[i])
mail.Send()
# Folder of attachments
path = r"C:\Users\Desktop "
# Address spreadsheet
addr = pd.read_excel(r"C:\Users\Desktop\address.xlsx")
for j in range(len(addr.Code)):
dirlist = []
# Address spreadsheet includes “Code” which is the recipient’s name/ID, and “add”, the email address
sub_code = addr.Code[j].astype("str")
subjects = sub_code + " - invoices"
# If the file names include the recipient’s ID at the start of 4 letters, the file(s) would be added to the list
for dirpath,dirname,filename in os.walk(path):
print(addr.Code[j].astype("str"))
for i in filename:
if i[0:4] == sub_code:
dirlist.append(os.path.join(dirpath,i))
# Send emails to multiple recipients with corresponding attachments
sendmail(add r["add"][j], dirlist, subjects)
I use this to send every recipient with multiple invoices.
Upvotes: 0