I have two dataframes df1 and df2. I am trying create a table from dataframe df2 and insert it in the body of email content . My current code is only taking records matching to Number=123 and creating tables in the body.While Subject is iterated correctly and the email is created correctly.
What is that I making wrong in the iterations of the rows . I am attaching code below
df1
Subject Number
Hello David Bill is due 123
Hello Adam Bill is due 456
Hello James Bill is due 789
df2
Number Month Amount
123 Jan 1oo
123 March 220
123 June 212
456 Jan 1oo
456 Feb 230
789 June 400
789 July 650
My code
import os
import boto3
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.utils import formatdate, COMMASPACE
from tabulate import tabulate
def create_message(send_from, send_to, cc_to, subject, plain_text_body):
message = MIMEMultipart("alternative", None, [MIMEText(html,'html')])
message['From'] = send_from
if str(send_to).__contains__(","):
message['To'] = COMMASPACE.join(send_to)
else:
message['To'] =send_to
message['Cc'] = cc_to
message['Date'] = formatdate(localtime=True)
message['Subject'] = subject
message.attach(MIMEText(plain_text_body, 'plain'))
return message
def send_message(message):
#print(message)
client = boto3.client("ses",region_name='eu-west-1')
response = client.send_raw_email(RawMessage = {"Data": message.as_string()})
html ='''
<p>Dear receiver,</p>
<p>Please find below the details</p>
{table}
<p> </p>
<p> </p>
<p>Thanks and best regards</p>
<p>Rahul.</p>'''
for i, row in df1.iterrows():
subject = row["Subject"]
to_address ="[email protected]"
cc_list = "[email protected]"
send_from="[email protected]"
df3=df2[df2['Number']==row['Number']]
headers= ['Number','Month','Amount']
html = html.format(table=tabulate(df3, headers, tablefmt="html"))
message = create_message(send_from,to_address, cc_list, subject,html)
send_message(message)
Expected output
Email1
Subject: Hello David Bill is due
Body of email
Please find below the details
Number Month Amount
123 Jan 1oo
123 March 220
Email2
Subject: Hello Adam Bill is due
Body of email
Please find below the details
Number Month Amount
456 Jan 1oo
456 Feb 230
Any help Appreciated
df3 = pd.merge(df1,df2,on='Number',how='inner')subjectsas expected but the table for each emails are created from row value. i.e in all the tables are having only123df3=df2[df2['Number...trydf3 = df2[df2['Number'].isin([row['Number']])]