Home > Blockchain >  How to include variable in HTML code inside Python for loop?
How to include variable in HTML code inside Python for loop?

Time:01-07

df = pd.read_excel('Email_List.xlsx')
receiver_email = df['EMAIL ID'].values
name = df['NAME'].values


zipped = zip(receiver_email,name)

for(a,b) in zipped:
    msg = EmailMessage()
    files = ['a_v2.xlsb','b_v2.xlsb','c_v2.xlsb',
         'd_v2.xlsb']

msg['Subject'] = 'Consumer Analytics Files' 
msg['From'] = Email_Address
msg['To'] = a
msg.add_alternative("""\
<!DOCTYPE html>

<html>

    
    
    <P>
    
       Hello {b}, <BR><BR>
    
       PFA of the Consumer Analytics files. Kindly review and please let me know if   there are any concerns. <BR><BR>
       
       Thank you! <BR><BR>
    
    
    
        Regards, <BR>
         Joe <BR>
         Quantitative Analyst <BR>
         ABC Technologies <BR>
         434 North Lane Dr <BR>
         Augusta, GA 30318 <BR>
        
    </P>
    
    
    
</html>      

""", subtype = 'html')

for file in files:
    
    with open(file,'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(Email_Address,Email_Password)
    smtp.send_message(msg)
        
print('All mail sent!')    

I want to include the variable 'b' inside the HTML code. In the above code the different names referred by variable b is not getting outputted. Please help. Also the msg.set_content() doesn't work when the html code is written.

The below code doesn't work when I use msg.add_alternative(). msg.set_content(f"Hello {b}: \n\n PFA of the Consumer Analytics files. Kindly review and please let me know if there are any concerns.\n\n ")

Please let me know how to access the name variable 'b' inside the html code. Thanks. I am on a Macbook.

CodePudding user response:

""".format(b), subtype = 'html'). 

Adding .format(b) does the trick.

CodePudding user response:

Try this. Made a syntax correction from (a,b) to just a,b and added f in front of the string of html.

df = pd.read_excel('Email_List.xlsx')
receiver_email = df['EMAIL ID'].values
name = df['NAME'].values

zipped = zip(receiver_email,name)

for a,b in zipped:
    msg = EmailMessage()
    files = ['a_v2.xlsb','b_v2.xlsb','c_v2.xlsb',
         'd_v2.xlsb']

msg['Subject'] = 'Consumer Analytics Files' 
msg['From'] = Email_Address
msg['To'] = a
msg.add_alternative(f"""\
<!DOCTYPE html>
<html>    
    <P>
       Hello {b}, <BR><BR>
    
       PFA of the Consumer Analytics files. Kindly review and please let me know if there are any concerns. <BR><BR>
       
       Thank you! <BR><BR>
    
        Regards, <BR>
         Joe <BR>
         Quantitative Analyst <BR>
         ABC Technologies <BR>
         434 North Lane Dr <BR>
         Augusta, GA 30318 <BR>
    </P>
</html>      
""", subtype = 'html')

for file in files:
    with open(file,'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(Email_Address,Email_Password)
    smtp.send_message(msg)
        
print('All mail sent!')   
  • Related