Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to link email in HTML

Understanding the Basics

HTML, which stands for Hyper Text Markup Language, is the standard markup language used in creating web pages. It has various features that allow us to make our website interactive and user-friendly. One such feature is the ability to link an email address, which we'll dive into in this blog post.

In simple terms, think of HTML as a set of building blocks that one can use to construct a website. Each block or element has a specific function and can be customized to fit the desired outcome. Just like building with legos, the more you understand about each block, the better your final structure will be.

The mailto link is the first step in linking an email in HTML. This is a built-in functionality in HTML that allows us to link to an email address directly. When a user clicks on a mailto link, their default email client will open with a new email drafted to the linked email address.

Here's a basic example of how to use the mailto link:

<a href="mailto:example@example.com">Email me</a>

In this code, a is an HTML element used for creating hyperlinks. href is an attribute of a that specifies the URL. In this case, instead of a URL, we put mailto:example@example.com. mailto: is a protocol that tells the browser to open the user's email client.

Adding a Subject Line

While the basic mailto link opens up an email, we can further customize it to auto-fill the subject and even the body of the email. This can be particularly useful if you want to guide the user in what to include in the email.

The subject can be added using ?subject= after the email address. Here's how you do it:

<a href="mailto:example@example.com?subject=Hello">Email me</a>

In this case, when the user clicks on "Email me", their email client will open up a new email drafted to example@example.com with the subject as Hello.

Adding a Body

Adding a body to the email is similar to adding a subject. Instead of ?subject=, we use &body=. Here's an example:

<a href="mailto:example@example.com?subject=Hello&body=How are you?">Email me</a>

Now, not only is the subject auto-filled to be Hello, but the body of the email will also be auto-filled with How are you?.

Adding Multiple Recipients

To add multiple recipients, you simply separate the email addresses with a comma, like so:

<a href="mailto:example1@example.com,example2@example.com">Email us</a>

This will open an email drafted to example1@example.com and example2@example.com.

Encoding Special Characters

Remember when typing the body of the email, certain characters like spaces, commas, and others have to be encoded. Spaces can be replaced with %20, and commas can be replaced with %2C.

For example:

<a href="mailto:example@example.com?subject=Hello&body=How%20are%20you%3F">Email me</a>

This will replace the space with %20 and the question mark with %3F.

Conclusion

Linking an email in HTML with the mailto link is a simple and effective way to provide a method of contact on a webpage. It provides a seamless experience for the user, as they simply have to click the link to draft an email.

Mastering this, along with other HTML elements and their attributes, will significantly improve your web development skills. Remember, just like building with legos, the more blocks you understand, the better your final structure will be. Happy coding!