Skip to content

Tag: SMTP Email

How to Send HTML Emails with Python

Python Logo

Python provides libraries like smtplib and email that make it relatively straightforward to compose and send HTML emails. In this guide, we’ll walk through the process step by step, including code examples and explanations.

Set up your SMTP server credentials

Before sending emails via Python, you’ll need to have access to an SMTP (Simple Mail Transfer Protocol) server. This could be your company’s email server, a third-party service like Gmail, or any other SMTP server that you have permission to use. You’ll need the server address, port, and your login credentials (username and password) for authentication.

Install required libraries:

Make sure you have the necessary Python libraries installed. You’ll need smtplib for sending emails and email for composing the email message.

You can install these libraries using pip:

pip install secure-smtplib
pip install emails

Compose the HTML email:

Create the HTML content for your email. You can use any HTML editor or simply write the HTML code directly in your Python script. Here’s a basic example:

html_content = """
<html>
<head></head>
<body>
    <h1>Hello!</h1>
    <p>This is a <strong>test email</strong> sent using Python.</p>
</body>
</html>
"""

Create the email message:

Use the email.message.EmailMessage class to create your email message. Set the From, To, Subject, and Content-Type headers appropriately.

import email.message
 
msg = email.message.EmailMessage()
msg["From"] = "your_email@example.com"
msg["To"] = "recipient@example.com"
msg["Subject"] = "ScriptWizards.net Test HTML Email"
msg.set_content(html_content, subtype="html")

Connect to the SMTP server and send the email:

Now, you’ll establish a connection to the SMTP server and send the email using the smtplib library. Replace "smtp.example.com" with your SMTP server address and "your_username" and "your_password" with your login credentials.

import smtplib
 
with smtplib.SMTP("smtp.example.com", 587) as server:
    server.starttls()
    server.login("your_username", "your_password")
    server.send_message(msg)

Full code example

Here’s the full code combining all the steps:

import smtplib
import email.message
 
# HTML content for the email
html_content = """
<html>
<head></head>
<body>
    <h1>Hello!</h1>
    <p>This is a email from <strong>ScriptWizards.Net</strong> sent using Python.</p>
</body>
</html>
"""
 
# Create the email message
msg = email.message.EmailMessage()
msg["From"] = "your_email@example.com"
msg["To"] = "recipient@example.com"
msg["Subject"] = "ScriptWizards.net Test HTML Email"
msg.set_content(html_content, subtype="html")
 
# Connect to the SMTP server and send the email
with smtplib.SMTP("smtp.example.com", 587) as server:
    server.starttls()
    server.login("your_username", "your_password")
    server.send_message(msg)

Conclusion

By following these steps, you can easily send HTML emails programmatically using Python. Whether you’re sending newsletters, marketing campaigns, or personalized notifications, Python provides the tools you need to automate the process and engage with your audience effectively. Remember to handle sensitive information like passwords securely and adhere to email sending best practices to avoid being flagged as spam.

© 2024 ScriptWizards.net - Powered by Coffee & Magic