0

I have the following code:

def smtp_mailer(name,email):
    html = """\
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
 <style type="text/css">
.h1, .p, .bodycopy {color: #153643; font-family: sans-serif;}
  .h1 {font-size: 33px; line-height: 38px; font-weight: bold;}
</style>
</head>
<body>

<h1>This is a {name}</h1>
<p>This is a {email).</p>

</body>
</html> 
"""
    part2 = MIMEText(html, 'html')
    msg.attach(part2) 

When I send SMTP mail then I am not able to pass value inside HTML at h1 tag for name and at p tag for email. Please help me out. Thank you.

4
  • Try substituting the values using html.format(name=...., email=....) Commented Jun 6, 2020 at 22:11
  • You probably forgot the f at the beginning to do the interpolation f”””text {name} “”” Commented Jun 7, 2020 at 0:41
  • But I have these inline CSS inside my HTML which is also asking for format ''' .header { width: 100%; background-color: #3B5998; height: 35px; margin-top:0; } .style1 { margin-right: 38px; } .style2 { width: 100%; background-color: #3B5998; height: 35px; } ''' @alaniwi Commented Jun 7, 2020 at 2:26
  • @anuragkumaranu Okay, I understand the problem now. Please see my answer. Commented Jun 7, 2020 at 5:35

2 Answers 2

1

After the initial comments, it seems that the main problem being asked about is the fact that the string contains some braces (that is, {} characters) that are intended to remain in the string as literals as part of the <style> element, but also ones which are intended to be substituted as part of the format specifier.

Given that this string is your own code that you are able to modify to what should be expected, rather than an externally supplied value where you would have to find an inevitably messy workaround for this inconsistency, the appropriate solution is to change the { and } that are intended to be literals into {{ and }}, so that after calling the format method they will be output as single braces.

After doing this (and also correcting the wrong character at the end of {email)), the format method can be applied:

For example,

html = """\
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
 <style type="text/css">
.h1, .p, .bodycopy {{color: #153643; font-family: sans-serif;}}
  .h1 {{font-size: 33px; line-height: 38px; font-weight: bold;}}
</style>
</head>
<body>

<h1>This is a {name}</h1>
<p>This is a {email}.</p>

</body>
</html> 
""".format(name="John", email="[email protected]")

print(html)

Gives:

...
.h1, .p, .bodycopy {color: #153643; font-family: sans-serif;}
  .h1 {font-size: 33px; line-height: 38px; font-weight: bold;}
...
<h1>This is a John</h1>
<p>This is a [email protected].</p>
...

As others have mentioned (in Python 3) you can also use the f string notation instead of explicitly calling the format method, provided that you have the values to be substituted already in variables called name and email. In any case, you will still need the literal braces to be doubled prior to formatting.

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

Comments

1

You can use string interpolation like so:

def smtp_mailer(name,email):
    html = f"""\
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a {name}</h1>
<p>This is a {email}.</p>

</body>
</html> 
"""
    print(html)

smtp_mailer("test", "[email protected]")

Returns

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a test</h1>
<p>This is a [email protected].</p>

</body>
</html>

1 Comment

But I have these inline CSS inside my HTML which is also asking for format ''' .header { width: 100%; background-color: #3B5998; height: 35px; margin-top:0; } .style1 { margin-right: 38px; } .style2 { width: 100%; background-color: #3B5998; height: 35px; } '''

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.