0

I'm using a script that extracts data from a text file, works on a Python script and sends an email from Python. It has a built-in HTML so that the email that is sent has the format I want. My question is: Is there any way to insert Python variables into the HTML I have as a template? I attach the code that I am using to see if someone can help me. Thank you.

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

from smtplib import SMTP

from_address = "[email protected]"
to_address = "[email protected]"
message = MIMEMultipart("alternative")

name = "Pepito"
number = "123456789"
email = "[email protected]"

text = "Nuevo Lead"
html = """\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

 <head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Nuevo Lead</title>

<meta name="viewport" content="width=device-width, initial-scale=1.0"/>

</head>

<body style="margin: 0; padding: 0;">
 <table border="0" cellpadding="0" cellspacing="0" width="100%">
 <tr>
  <td align="center">
  <img src="https://image.ibb.co/caP9CG/company_logo_blanco.jpg" alt="company_logo_blanco" border="0"></a>  
</td>
 </tr>
<tr>
<td align="center">
<h1> Nuevo Lead </h1>
</td>
</tr>
<tr>
<td>
Nombre:
</td>
</tr>
<tr>
<td>
Telefono:
</td>
</tr>
<tr>
<td>
Email:
</td>
</tr>
</table>
 </body>
</html>
"""

part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")

message.attach(part1)
message.attach(part2)

message["From"] = "Nuevo Lead!"
message["To"] = to_address
message["Subject"] = "Correo de prueba"

smtp = SMTP("Server")
smtp.login(from_address, "Password")

smtp.sendmail(from_address, to_address, message.as_string())

smtp.quit()
1
  • this html is a normal string. You can put variables like: 'foo {}'.format('bar'). If you are using python 3.6 you can use the new format string style. Commented Jan 4, 2018 at 16:03

1 Answer 1

1

A straightforward solution is to use string Templates. They're safe and designed for this purpose. In this snippet, we create a template with all our html code using the string.Template constructor, and then the .substitute() method returns our html code when we specify values for the placeholders in the template.

from string import Template

html_template = Template("""My name is $name""")
html = html_template.substitute(name='Pepito')

Alternatively, for Python 3.6+, f-strings aren't necessarily bad either. They essentially allow you to execute arbitrary python code inside your strings. In this case, we have some sort of name defined ahead of time, we specify the string is an f-string by appending an f to the front, and everything in curly braces is executed as raw python code -- in this case replacing {name} with the contents of the name variable.

name = 'Pepito'
html = f"""My name is {name}"""

One advantage these methods have over normal string formatting is that in longer html documents, you are likely to reuse each variable more than once, and it is convenient to be able to call them by name rather than trying to recall the exact order you placed them into whatever you're calling a template string. This makes it more maintainable in the future as well, should you ever decide to change what the html code will look like.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank You! Worked perfectly
Is there any way to make Python change the value of a variable, taking the name of the last file created in a folder as parameter? Thank you.
The accepted answer written by Mark Amery for file creation/modification dates is relatively complete and has a nice code snippet: stackoverflow.com/questions/237079/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.