I have one dataframe that contain column email
and other columns.
I need to send this data to corresponding email, for example line 0 need to be emailed to Tony@mail.com
,
line 1 need to be emailed to Sergio@mail.com
and line 2 and 3 need to be send to Nico@mail.com
Name Email Subject CreatedDate DueDate FinalDeadLine
0 Tony Tony@mail.com Renewal 2019-12-15 2019-12-16 2019-12-25
1 Sergio Sergio@mail.com NewBusiness 2019-11-18 2019-11-22 2019-11-28
2 Nico Nico@mail.com Endorsement 2019-12-11 2019-12-13 2019-12-24
3 Nico Nico@mail.com Rewrite 2019-12-05 2019-12-07 2019-12-23
Using Splitting dataframe into multiple dataframes I am performing a view on a dataframe:
Example code:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pandas as pd
df = pd.DataFrame({
'Name': ['Tony','Sergio','Nico','Nico']
,'Email': ['Tony@mail.com', 'Sergio@mail.com','Nico@mail.com','Nico@mail.com']
,'Subject':['Renewal', 'NewBusiness', 'Endorsement','Rewrite']
,'CreatedDate': ['2019-12-15','2019-11-18','2019-12-11','2019-12-05']
,'DueDate': ['2019-12-16','2019-11-22','2019-12-13','2019-12-07']
,'FinalDeadLine': ['2019-12-25','2019-11-28','2019-12-24','2019-12-23']
})
print(df)
# sort the dataframe
# the parameter axis=1 refer to columns, while 0 refers to rows
df.sort_values(by='Email', axis=0, inplace=True)
# set the index to be this and don't drop
df.set_index(keys=['Email'], drop=False,inplace=True)
# get a list of emails
email_list=df['Email'].unique().tolist()
# now we can perform a lookup on a 'view' of the dataframe
nico = df.loc[df.Email=='Nico@mail.com']
# itrating through email_list and printing dataframe corresponding to each email
for e in email_list:
d = df.loc[df.Email==e]
#print(d)
But then how can I connect this to my send_mail
function?
send_mail function:
user = "myemail@gmail.com"
pwd = "mypassword"
subject = "Test subject"
recipients = "recipients@gmail.com"
def send_email(user,pwd, recipients, subject):
try:
df_html = df.to_html()
dfPart = MIMEText(df_html,'html')
#Container
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = user
msg['To'] = ",".join(recipients)
msg.attach(dfPart)
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(user, pwd)
server.sendmail(user, recipients, msg.as_string())
server.close()
print("Mail sent succesfully!")
except Exception as e:
print(str(e))
print("Failed to send email")
send_email(user,pwd,recipients,"Test Subject")
Or maybe there is a better and most efficient way of doing all this? Any good examples available online?