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:-

        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 Out Email via Python JoeDainton123 1 287 Aug-31-2020, 12:54 AM
Last Post: nilamo
  Including modules in Python using sys.path.append JoeDainton123 1 337 Aug-24-2020, 04:51 AM
Last Post: millpond
  Remove from end of string up to and including some character lbtdne 2 470 May-17-2020, 09:24 AM
Last Post: menator01
  Sending an email with attachment without using SMTP? PythonNPC 5 645 May-05-2020, 07:58 AM
Last Post: PythonNPC
  "Up to but not including" (My personal guide on slicing & indexing) Drone4four 5 596 Nov-20-2019, 09:38 PM
Last Post: newbieAuggie2019
  Loop through tags inside tags in Selenium/Python xpack24 1 1,225 Oct-23-2019, 10:15 AM
Last Post: Larz60+
  Applying row height to all rows including and after row 7 curranjohn46 2 922 Oct-14-2019, 03:10 PM
Last Post: curranjohn46
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 628 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  including the white space parts in str.split() Skaperen 6 793 Jun-20-2019, 06:03 PM
Last Post: Skaperen
  sending html file in email santoshi 3 1,447 Apr-05-2019, 03:59 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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