Python Forum
Including a Variable In the HTML Tags When Sending An Email
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Including a Variable In the HTML Tags When Sending An Email
#1
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:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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())
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending email using own smtp server robertkwild 11 5,613 Jul-05-2024, 12:59 AM
Last Post: AdamHensley
  Function to count words in a list up to and including Sam Oldman45 15 10,316 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  why doesn't it replace all html tags? Melcu54 3 1,821 Jul-05-2023, 04:47 AM
Last Post: Melcu54
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 1,816 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  BeautifulSoup - I can't translate html tags that contain <a href=..</a> OR <em></em> Melcu54 10 3,327 Oct-27-2022, 08:58 AM
Last Post: wavic
  Get text from within h3 html tags Pedroski55 8 6,551 Jan-05-2022, 06:50 AM
Last Post: Larz60+
  Including data files in a package ChrisOfBristol 4 4,444 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 18,660 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  reading html and edit chekcbox to html jacklee26 5 4,300 Jul-01-2021, 10:31 AM
Last Post: snippsat
  Parsing link from html tags with Python Melcu54 0 2,243 Jun-14-2021, 09:25 AM
Last Post: Melcu54

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020