How to Set the Subject and Content of an Email Using mailto: in Code

Read Time: 2 minutes

When creating websites or applications, it’s often useful to enable users to send emails directly from the interface. The mailto: protocol is a simple way to create a clickable link that opens a user’s default email client with a pre-defined subject, body content, and more. This is a convenient and straightforward method to guide the recipient on what the email should include.

Here’s a step-by-step guide to setting up the subject and content of an email using mailto: in code.

Basic mailto: Usage

At its core, mailto: is used to specify an email address to which the message should be sent. The basic syntax is as follows:

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

When the user clicks this link, their default email client will open with a new email addressed to someone@example.com.

Adding a Subject

To make the email even more structured, you can set the subject line by appending ?subject= after the email address. For example:

<a href="mailto:someone@example.com?subject=Hello%20There">Send Email</a>

In this example, clicking the link will open a new email to someone@example.com with “Hello There” automatically inserted as the subject. Note that spaces and special characters need to be URL-encoded, meaning a space becomes %20.

Adding Body Content

You can also pre-fill the body of the email by adding &body= to your mailto: link. This helps streamline communication, particularly when you want users to submit certain details. For example:

<a href="mailto:someone@example.com?subject=Hello%20There&body=I%20hope%20this%20email%20finds%20you%20well.">Send Email</a>

This link will open an email addressed to someone@example.com, with “Hello There” as the subject and the following message in the body: “I hope this email finds you well.”

Combining Multiple Parameters

It’s possible to combine more parameters, like including a CC or BCC recipient. This can be done by appending &cc= and &bcc= after the email address or subject. Here’s an example of how to combine everything:

<a href="mailto:someone@example.com?cc=ccperson@example.com&bcc=bccperson@example.com&subject=Meeting%20Request&body=I%20would%20like%20to%20schedule%20a%20meeting.">Send Email</a>

In this case:
The email will be sent to someone@example.com.
A CC will be sent to ccperson@example.com.
A BCC will be sent to bccperson@example.com.
The subject will be “Meeting Request.”
The body will include “I would like to schedule a meeting.”

Handling Special Characters

As mentioned earlier, special characters such as spaces, punctuation, or symbols must be encoded using URL encoding. For example:

A space becomes %20.
A newline can be represented by %0A or %0D%0A.

Here’s an example with multiple lines in the email body:

<a href="mailto:someone@example.com?subject=Feedback&body=Dear%20Team,%0A%0AThank%20you%20for%20your%20support.%0A%0ABest%20regards,">Send Email</a>

This will create an email with a line break between the “Dear Team,” and “Thank you for your support.”

Limitations of mailto:

Although the mailto: feature is a great way to pre-fill email information, it does have limitations:

It depends on the user having a default email client configured on their device.
The length of the URL is limited by browsers and email clients, so overly long subjects or bodies might not work.
Advanced formatting (e.g., bold text, attachments) isn’t supported with mailto:.

Using mailto: in your code can make sending emails easier for users by pre-filling the subject, body, and other details. It simplifies interactions and ensures that you get the information you need with less back-and-forth. Just remember to always URL-encode your text properly, and keep in mind the limitations of this simple, yet powerful tool.

Was this article helpful?
YesNo
Translate »