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 = "santiago@madketing.com.ar"
to_address = "santiago@madketing.com.ar"
message = MIMEMultipart("alternative")

name = "Pepito"
number = "123456789"
email = "pepito@pepito.com"

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()
  • 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. – Pentux Jan 04 '18 at 16:03

1 Answers1

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.

Hans Musgrave
  • 6,613
  • 1
  • 18
  • 37
  • Thank You! Worked perfectly – Santiago Ruffino Jan 05 '18 at 12:29
  • 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. – Santiago Ruffino Jan 05 '18 at 14:54
  • The accepted answer written by Mark Amery for file creation/modification dates is relatively complete and has a nice code snippet: https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python – Hans Musgrave Jan 05 '18 at 20:25