Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Sending an HTML e-mail using Python
When you send a text message using Python, all the content is treated as simple text. Even if you include HTML tags in a text message, it is displayed as simple text and HTML tags will not be formatted according to HTML syntax. However, Python provides the option to send an HTML message as actual HTML content.
While sending an email message, you can specify a MIME version, content type and character set to send an HTML email that renders properly in email clients.
Basic HTML Email Structure
To send HTML email, you need to set the proper headers in your message ?
import smtplib
# HTML email headers and content
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML Email Test
<html>
<head></head>
<body>
<p>This is an email message sent in HTML format</p>
<b>This is HTML message.</b>
<h1>This is headline.</h1>
</body>
</html>
"""
print("HTML email message created successfully")
HTML email message created successfully
Complete HTML Email Example
Here's a complete example that sends HTML content as an email ?
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_html_email():
# Email configuration
sender_email = "sender@example.com"
sender_password = "your_password"
receiver_email = "receiver@example.com"
# Create message container
msg = MIMEMultipart("alternative")
msg["Subject"] = "HTML Email Test"
msg["From"] = sender_email
msg["To"] = receiver_email
# Create HTML content
html = """
<html>
<body>
<h1>Welcome to Our Newsletter</h1>
<p>This is an <b>HTML email</b> sent using Python.</p>
<ul>
<li>Feature 1</li>
<li>Feature 2</li>
<li>Feature 3</li>
</ul>
<p>Visit our <a href="https://example.com">website</a></p>
</body>
</html>
"""
# Convert to MIMEText object
html_part = MIMEText(html, "html")
msg.attach(html_part)
# Send email
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, msg.as_string())
server.quit()
print("HTML email sent successfully!")
except Exception as e:
print(f"Error: {e}")
# Call the function
send_html_email()
Key Components
Essential elements for HTML email ?
- MIME-Version: 1.0 − Specifies MIME version
- Content-type: text/html − Tells email client to render as HTML
- HTML structure − Proper <html>, <body> tags
- Email headers − From, To, Subject fields
Using MIMEText for Better Control
The email.mime.text module provides better HTML email handling ?
from email.mime.text import MIMEText
# Create HTML content
html_content = """
<html>
<body>
<h2 style="color: blue;">Styled HTML Email</h2>
<p>This email has <em>inline CSS styling</em>.</p>
<table border="1">
<tr><th>Name</th><th>Age</th></tr>
<tr><td>Alice</td><td>25</td></tr>
<tr><td>Bob</td><td>30</td></tr>
</table>
</body>
</html>
"""
# Create MIMEText object
html_msg = MIMEText(html_content, "html")
html_msg["Subject"] = "HTML Table Email"
html_msg["From"] = "sender@example.com"
html_msg["To"] = "receiver@example.com"
print("MIMEText HTML email object created")
print("Content-Type:", html_msg.get_content_type())
MIMEText HTML email object created Content-Type: text/html
Conclusion
To send HTML emails in Python, set the content type to "text/html" and include proper HTML structure. Use the email.mime.text module for better control and formatting of HTML email content.
