1

I have a function which sends a mail to a user, like this:

import os
import smtplib
import imghdr
from email.message import EmailMessage
EMAIL_ADDRESS = email
EMAIL_PASSWORD = password
msg = EmailMessage()
msg['Subject'] = 'Email Title'
msg['From'] = EMAIL_ADDRESS
msg['To'] = ANOTHER_EMAIL_ADDRESS
msg.set_content('Email content')
html = open('email.html', 'r').read()
print(html)
msg.add_alternative(html, subtype='html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
 smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
 smtp.send_message(msg)

And the html file is like the following:

<!DOCTYPE html>
<body>
 <h3 style="font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;font-size: large;font-weight: bold;">Hey there, {{userEmail}}!
 <h3>
</body>
</html>

I want to be able to pass the 'userEmail' variable to the HTML file. How may I do that?

asked Sep 26, 2021 at 2:47

1 Answer 1

2

In email.html change {{userEmail}} to {userEmail}. Then you should be able to use Python string formatting to add the userEmail variable.

userEmail = "MyFriend"
html = open('email.html', 'r').read().format(userEmail=userEmail)
print(html)
answered Sep 26, 2021 at 2:54
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.