Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to send HTML email in gmail

Getting Started

If you're new to programming, sending an HTML email using Gmail may sound like a daunting task. However, it's not as complicated as it seems. In this blog, we'll walk through the process step-by-step. HTML, or HyperText Markup Language, is the standard language used to create web pages.

Understanding What HTML Email Is

Before we dive into the code, let's take a moment to understand what an HTML email is. Think of it as a mini web page packed into an email. It allows you to include things like images, links, and different font styles in your emails. It's kind of like the difference between a plain, text-only book and a colorful picture book.

Writing HTML for Email

Now, let's talk about how to write HTML for an email. Here's a basic example:

<!DOCTYPE html>
<html>
  <body>
    <h1>Welcome to My Website!</h1>
    <p>Click <a href="http://www.mywebsite.com">here</a> to visit my website.</p>
  </body>
</html>

In the above code, we have a heading (<h1>) and a paragraph (<p>). Inside the paragraph, we have a link (<a href="http://www.mywebsite.com">) that points to a website.

Preparing the Email Body

The next step is to prepare the body of the email. We need to use a programming language to do this. We'll use Python for our example because it's beginner-friendly and widely used.

First, you must have Python installed on your computer. If you haven't done that yet, you can download it from here.

Here's a simple Python script that prepares an email body:

html = """
<!DOCTYPE html>
<html>
  <body>
    <h1>Welcome to My Website!</h1>
    <p>Click <a href='http://www.mywebsite.com'>here</a> to visit my website.</p>
  </body>
</html>
"""

In this script, we're simply storing our HTML code in a Python variable called html.

Sending the Email

Now comes the most exciting part: sending the email.

Python provides a built-in module for this called smtplib. This module defines an object that can handle the Simple Mail Transfer Protocol (SMTP). SMTP is the main protocol used to send electronic mail on the Internet.

Here's a Python script that sends an HTML email:

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

# setup the parameters of the message
password = "your_password"
msg = MIMEMultipart()
msg['From'] = "your_email@gmail.com"
msg['To'] = "recipient_email@gmail.com"
msg['Subject'] = "This is a test"

# add in the message body
msg.attach(MIMEText(html, 'html'))

#create server
server = smtplib.SMTP('smtp.gmail.com: 587')

server.starttls()

# Login Credentials for sending the mail
server.login(msg['From'], password)

# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())

server.quit()

print ("successfully sent email to %s:" % (msg['To']))

In this script, we're doing several things:

  1. Importing the necessary modules.
  2. Setting up the message parameters (your email, your password, the recipient's email, and the subject line).
  3. Adding our HTML code to the message body.
  4. Creating a server that will send the email.
  5. Logging into the server using your email and password.
  6. Sending the email.
  7. Quitting the server.
  8. Printing a success message.

That's it! You've just sent an HTML email using Python and Gmail.

Remember, this is a basic example. You can add more complexity to your emails as you get more comfortable with the process. You could include images, style your text with CSS, or even include forms in your emails. But for now, congratulations on sending your first HTML email!

Wrap Up

In this blog, we've learned what an HTML email is, how to write HTML for an email, and how to send it using Python and Gmail. This is a powerful skill that can help you create more engaging and interactive emails. Happy coding!