Including a Variable In the HTML Tags When Sending An Email

Richard Damon Richard at Damon-Family.org
Sat Aug 8 10:45:44 EDT 2020


All the text that you want the user to see needs to be in the <body> of
the message. <head> is for metadata and stuff to setup some formatting.

You might want to study up a bit on HTML formatting too. Depending on
what the data frame is like, you may need to enclose it in some sort of
container, like a <div>

On 8/8/20 10:29 AM, sammy.jackson987 at gmail.com wrote:
> Hello all
>
> I was hoping someone could help me with the following coding problem.
>
> I am trying to send an email where the body of the email is taken from a data frame, which i have managed to do.
>
> However i want to start the email by saying Hi Name, where Name is a variable that contains the person's name to whom i am sending the email to - This is the bit i cannot get working.
>
> The code i have so far is as follows:-
>
> [python]
> import smtplib
> from email.mime.multipart import MIMEMultipart
> from email.mime.text import MIMEText
>              
> mail=smtplib.SMTP('smtp.gmail.com', 123) 
> mail.ehlo() 
> mail.starttls()  
> mail.login("Email","Pwd") 
>              
> From_Address = ["From_Email"]
> To_Address = [Report_Data_Frame.iloc[0,10]]
> CC_Address = ["CC_Email", "CC_Email", "CC_Email"]
> Subject_Email = "Email_Subject" 
> Body = Email_Body_Data_Frame
> Name = "Tom"
>          
>                
> html = """\
> <html>
>            
>   <head>
>       Hi Name Goes HERE!!!
>       <br>
>       <br>
>       TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT<br> <br>
>   </head>
>            
>   <body>
>            
>     {0}
>      
>   </body>
>            
>   <br>
>            
>  TEXT TEXT <br><br>
>  TEXT TEXT <br><br>
>  TEXT TEXT <br><br>
>  TEXT TEXT <br>
>           
>         
> </html>
>  """.format(Body.to_html())
>      
> msg = MIMEMultipart()
> msg['From'] = ', '.join(From_Address)
> msg['To'] = ', '.join(To_Address)
> msg['Cc'] = ', '.join(CC_Address)
> msg['Subject'] = Subject_Email
>              
> message = MIMEText(html,'html')
> msg.attach(message)
> mail.sendmail(From_Address, (To_Address + CC_Address), msg.as_string())
> [/python]
>
> In this case the variable Name is Tom and i want to include Tom in the email.
>
> Can anyone help?
>
> Still a newbie; approx 3 weeks playing with Python (cut and past most of this code)
>
> Any help will be greatly appericated.
>
> Thank you.


-- 
Richard Damon



More information about the Python-list mailing list