Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to link an email in HTML

A core aspect of HTML involves creating links that direct users to different parts of a website or to entirely different sites. To keep it simple, think of it as creating a portal that transports you from one place to another. Let's dive into how to create a link that directs to an email.

The basic syntax of an HTML link is <a href="URL">Link text</a>. To break it down:

  • <a> is the HTML tag that we use to make links.
  • href is an attribute we use with the <a> tag to specify the destination of the link.
  • URL is the destination of the link.
  • Link text is the clickable text that takes us to the destination.

Just imagine href as a signpost that points towards the destination (URL), and the Link text is the vehicle you use to get there.

Now, let's specifically look at how to create an email link in HTML. Email links, when clicked, open the user's default email client with a new email composed, ready to be sent to a specified email address.

The basic syntax of an HTML email link is <a href="mailto:someone@domain.com">Email me</a>. Here:

  • mailto: is a special type of protocol (think of it as a method or a system) that browsers understand to mean "open the email client".
  • someone@domain.com is the email address you want to link to.

When the link text ("Email me") is clicked, an email composing window opens, ready to send an email to someone@domain.com.

Adding a Subject to the Email

Sometimes, you might want to pre-populate the subject field of the email. You can do this by adding ?subject= followed by the subject text at the end of the email address.

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

Now, when the link is clicked, not only will a new email window open for someone@domain.com, but the subject of the email will also be pre-populated with "Hello".

Think of it like sending a letter with a pre-written address and subject line - all the recipient needs to do is write the main content and hit send!

Adding More Fields to the Email

You can also pre-populate other fields of the email, such as the CC, BCC, and body fields. This is done using the & symbol followed by the field name and its value.

For example: <a href="mailto:someone@domain.com?subject=Hello&cc=another@domain.com&bcc=third@domain.com&body=Nice%20to%20meet%20you!">Email me</a>.

In this example, the cc field is set to another@domain.com, the bcc field is set to third@domain.com, and the body of the email is pre-populated with "Nice to meet you!". The %20 is a URL-encoded space character.

Finally, you can also style your email links using CSS to make them more appealing or to match the design of your site. You do this by adding the style attribute to the <a> tag.

For example: <a href="mailto:someone@domain.com" style="color: red; text-decoration: none;">Email me</a>.

This will make the link text ("Email me") red and remove the underline from it.

Conclusion

Creating email links in HTML is a straightforward process, but with the right knowledge, you can create links that not only direct users to their email client, but also pre-populate various fields, saving them time and improving their experience on your site. As with any code, it's all about understanding how the pieces fit together and what each piece does. Happy coding!