1

I'm using the python framework, Flask, to send a HTML email with the following code:

HTML Email:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Email</title>
</head>
<body>
    <h1>This is a heading</h1>
    <p>This is a paragraph</p>
</body>
</html>

Flask (using Flask-Mail):

html = render_template('email/activate.html', confirmURL=confirmURL)
        msg = Message('Please confirm your email', sender='email@gmail.com', recipients=[email])
        msg.body = html
        mail.send(msg)

I kept it basic to test if the email was appearing correctly or not. I sent the email to a Gmail and an Outlook account but both seem to appear the HTML as just plain text. I've tried using different HTML snippets and the same result occurs. I'm not sure if it is a problem with my code, my email accounts or Flask.

Thanks in advance.

Pav Sidhu
  • 6,724
  • 18
  • 55
  • 110

1 Answers1

1

I figured it out myself the problem was this:

Before:

page = render_template('email/activate.html', confirmURL=confirmURL)
msg = Message('Please confirm your email', sender='pavsidhu98@gmail.com', recipients=[email])
msg.body(page)
mail.send(msg)

After:

page = render_template('email/activate.html', confirmURL=confirmURL)
msg = Message('Please confirm your email', html=page, sender='pavsidhu98@gmail.com', recipients=[email])
mail.send(msg)

So instead of putting the HTML in the email as msg.body(page) instead put it in as a parameter of Message() like so: html=page

Pav Sidhu
  • 6,724
  • 18
  • 55
  • 110