1

So I made this program to send HTML emails using python. The reason I made this was to send personalized emails easily. But can't find a way to embed python variables to HTML. Is it not possible?

Do I need javascript for this? I don't have any JS knowledge at all.

Please let me know how I can do this.

import smtplib, ssl,getpass
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#Mail server details
port=465
smtp_server="smtp.gmail.com"

#Account details
my_email="emailautomationpy@gmail.com"
password = getpass.getpass("Enter you password and press return\n")

#Receiver's details
receiver_email="emailautomationpy@gmail.com"

messeage= MIMEMultipart('alternative')
messeage["Subject"]= "Subject"
messeage["From"]= my_email
messeage["To"]= receiver_email


#HTML EMAIL

html = """\
<html>
  <body>
    <p>Hi,{name}<br>
       How are you?<br>
       
    </p>
  </body>
</html>
"""


messeage.attach(MIMEText(html,"html"))

context = ssl.create_default_context()

try:
    print("Sending your email... Please wait...")
    with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
        server.login(my_email, password)
        server.sendmail(
            my_email,receiver_email,messeage.as_string()
            )
    print("\n(*) Email sent successfully (*)") 
except:
    print("An error occurred while sending the email")

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
h3x
  • 41
  • 4
  • 1
    You can't just embed a variable in a regular string with `{}`. You need to use an _f-string_ to do that. Prepend `f`: `f"""..."""`. – ChrisGPT was on strike Jul 25 '20 at 11:39
  • 5
    Does this answer your question? [How do I put a variable inside a string?](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-inside-a-string) – ChrisGPT was on strike Jul 25 '20 at 11:41
  • 1
    I can't use f-string because there's CSS in my original HTML document. So there's a lot of ```{}``` in that. edit: I know I can't use ```{}``` to embed variables, I just put it there to show what I want! – h3x Jul 25 '20 at 12:09
  • 1
    Then use one of the other (several) valid string formatting techniques listed in the link I provided. Or escape your literal braces by doubling them up. See https://stackoverflow.com/q/5466451/354577 – ChrisGPT was on strike Jul 25 '20 at 12:22
  • 1
    Or, if you want a bit more power, pick any of the (several) [templating libraries](https://duckduckgo.com/?q=python+templating+library) out there: Jinja2, Mako, Cheetah, [the template strings included in the standard library](https://docs.python.org/3/library/string.html#template-strings), whatever, and use that. – ChrisGPT was on strike Jul 25 '20 at 12:24
  • 1
    Thank you, I solved it using ```%s``` formatting – h3x Jul 25 '20 at 13:14

1 Answers1

1

You can use the string format function in python

email_str = 'Hello {name}, welcome to {site_name}'.format(name = “Customer”, site_name = “Our Service”)
Hussein Fawzy
  • 366
  • 2
  • 16