2026-07-15

how to add href link in email content when sending email through smtplib

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

how to add href link in email content when sending email through smtplib

Mastering Email Formatting: How to Add Hyperlinks (href) When Sending via SMTP

Sending emails programmatically using libraries like Python's smtplib often involves dealing with the nuances of MIME encoding. While sending plain text is straightforward, embedding rich content, such as HTML with hyperlinks, requires careful attention to formatting and encoding. Many developers run into issues where their intended HTML tags are displayed literally rather than rendered as clickable links in the recipient's inbox.

This post will dissect the problem you encountered—how to correctly inject href links into email content when using MIMEText for sending, ensuring your messages are delivered exactly as intended.

The Root of the Problem: MIME Escaping and Text vs. HTML

The issue you observed stems from how the email system (and subsequent mail clients) interpret the raw data being transmitted. When you use msg = MIMEText(...), you are essentially defining the content type for that part of the message. If you pass a string containing <a href="..."> directly, some systems treat this as literal text rather than interpreted HTML structure, leading to the escaping you saw (&lt;a href...).

In essence, the email client expects properly formatted MIME parts. To make an email be recognized as HTML content that needs rendering, you must explicitly define the Content-Type header correctly and ensure the body string itself is valid HTML.

The Correct Approach: Using MIMEText for HTML Content

To successfully embed clickable links in an email, you must signal to the recipient's mail client that the message content is formatted as HTML. This involves constructing a full MIME message structure, not just dumping the text into a single MIMEText object.

Here is how you should construct your message payload to ensure hyperlinks render correctly:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 1. Construct the HTML content you want to send
html_content = """
<html>
<body>
    <p><a href="http://www.google.com" rel="noreferrer">abc</a></p>
</body>
</html>
"""

# 2. Create a multipart message structure (essential for HTML)
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Email with Hyperlink Test'
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'

# Attach the HTML content to the message
part1 = MIMEText(html_content, 'html')
msg.attach(part1)

# 3. Send the email (SMTP connection setup omitted for brevity)
# s = smtplib.SMTP('smtp.example.com', 25)
# s.sendmail('sender@example.com', 'recipient@example.com', msg.as_string())
print("Message successfully constructed using MIME structure.")

Explanation of Best Practices

  1. MIMEMultipart('alternative'): When sending a message that contains both plain text and HTML versions (which is best practice for email compatibility), we use MIMEMultipart. The 'alternative' subtype tells the client that there are different representations of the same content available.
  2. Explicit Encoding ('html'): By explicitly setting the subtype to 'html' when creating the MIMEText part, you instruct the email client how to decode and render the body content. This bypasses the literal text escaping problem you encountered previously.
  3. Full HTML Structure: Ensure your content is wrapped in standard HTML tags (<html>, <body>) for maximum compatibility across various email clients.

Conclusion: Structuring Data for Reliable Communication

Developing robust systems, whether building backend APIs or handling external communications like email, hinges on understanding the protocols and encoding standards involved. Just as structuring data correctly is vital when working with frameworks like Laravel where clear Eloquent models translate cleanly to JSON responses, structuring your MIME message correctly ensures reliable communication. By moving beyond simple MIMEText dumps and embracing the full structure of multipart messages, you gain control over how your content is interpreted by every mail client globally. Always prioritize explicit type definition when sending rich content via SMTP.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.