Can I set subject/content of email using mailto:?

979,891

Solution 1

Yes, look all tips and tricks with mailto: http://www.angelfire.com/dc/html-webmaster/mailto.htm

mailto subject example:

<a href="mailto:[email protected]?subject=free chocolate">example</a>

mailto with content:

<a href="mailto:[email protected]?subject=look at this website&body=Hi,I found this website and thought you might like it http://www.geocities.com/wowhtml/">tell a friend</a>

As alluded to in the comments, both subject and body must be escaped properly. Use encodeURIComponent(subject) on each, rather than hand-coding for specific cases.

As Hoody mentioned in the comments, you can add line breaks by adding the following encoded sequence in the string:

%0D%0A // one line break

Solution 2

<a href="mailto:[email protected]?subject=Feedback for 
webdevelopersnotes.com&body=The Tips and Tricks section is great
&[email protected]
&[email protected]">Send me an email</a>

you can use this code to set subject, body, cc, bcc

Solution 3

I created an open-source tool for making this easy. Enter the strings you want and you'll instantly get the mailto:

mailto.now.sh

💌⚡️ Template full emails in a mailto

enter image description here

Solution 4

The mailto: URL scheme is defined in RFC 2368. Also, the convention for encoding information into URLs and URIs is defined in RFC 1738 and then RFC 3986. These prescribe how to include the body and subject headers into a URL (URI):

mailto:[email protected]?subject=current-issue&body=send%20current-issue

Specifically, you must percent-encode the email address, subject, and body and put them into the format above. Percent-encoded text is legal for use in HTML, however this URL must be entity encoded for use in an href attribute, according to the HTML4 standard:

<a href="mailto:[email protected]?subject=current-issue&amp;body=send%20current-issue">Send email</a>

And most generally, here is a simple PHP script that encodes per the above.

<?php
$encodedTo = rawurlencode($message->to);
$encodedSubject = rawurlencode($message->subject);
$encodedBody = rawurlencode($message->body);
$uri = "mailto:$encodedTo?subject=$encodedSubject&body=$encodedBody";
$encodedUri = htmlspecialchars($uri);
echo "<a href=\"$encodedUri\">Send email</a>";
?>

Solution 5

You can add subject added to the mailto command using either one of the following ways. Add ?subject out mailto to the mailto tag.

<a href="mailto:[email protected]?subject=testing out mailto">First Example</a>

We can also add text into the body of the message by adding &body to the end of the tag as shown in the below example.

 <a href="mailto:[email protected]?subject=testing out mailto&body=Just testing">Second Example</a>

In addition to body, a user may also type &cc or &bcc to fill out the CC and BCC fields.

<a href="mailto:[email protected]?subject=testing out mailto&body=Just testing&[email protected]&[email protected]">Third
    Example</a>

How to add subject to mailto tag

Share:
979,891
Jiew Meng
Author by

Jiew Meng

Web Developer &amp; Computer Science Student Tools of Trade: PHP, Symfony MVC, Doctrine ORM, HTML, CSS, jQuery/JS Looking at Python/Google App Engine, C#/WPF/Entity Framework I hope to develop usable web applications like Wunderlist, SpringPad in the future

Updated on May 11, 2020

Comments

  • Jiew Meng
    Jiew Meng almost 4 years

    Is it possible to set the subject/content of email when I use mailto:?

  • Jiew Meng
    Jiew Meng about 13 years
    is it possible to use HTML for the body?
  • tripleee
    tripleee almost 11 years
    That is entirely client specific. In the general case, no.
  • jocull
    jocull over 10 years
    You may be able to add HTML into the body if you URL encode the entire string. You'd need to test and make sure it works well in a majority of email clients.
  • Oscar Pérez
    Oscar Pérez over 10 years
    Just as a reference: you don't have to htmlescape the subject
  • Simon East
    Simon East about 10 years
    Does anyone know a reference that outlines the mail client support for this?
  • gordie
    gordie about 9 years
    Note that W3C requires that you encode your string following those rules : Characters should be represented in NFC and spaces should be escaped as %20.
  • Armfoot
    Armfoot over 8 years
    Also a very useful feature of this href field composer is the ability to encode all characters to hexadecimal (e.g.: [Test Zõne] becomes %5BTest%20Z%C3%B5ne%5D, this may avoid some spam bots).
  • Armfoot
    Armfoot over 8 years
    I mean, if you choose to encode all characters, they are all converted to HTML notation &#x (avoiding some spam bots).
  • varun aaruru
    varun aaruru about 8 years
    this question was answered 5 years ago
  • quemeful
    quemeful about 8 years
    and it still is used today when you wanna mail something
  • dakab
    dakab almost 8 years
    That angelfire.com link is unavailable. Can you find a replacement for it? If not, it may be removed from the answer. What information were there?
  • Haim Evgi
    Haim Evgi almost 8 years
    i can acsess the link, what you see
  • David
    David almost 8 years
    My subject is not text. It is a table. How to add that.
  • Quentin
    Quentin over 7 years
    The question is asking how to set the subject and content. It didn't mention formatting. While that is a useful thing to note on an answer, you left out the bit about what is possible and how to achieve it. Without that, this is a comment, not an answer.
  • Capsule
    Capsule over 7 years
    You know this angelfire link is probably 15 years old, right?
  • nafg
    nafg over 7 years
    I don't think the space should be there?
  • Namkce
    Namkce over 6 years
    is there still a 256 char limit to the mailto string?
  • superphonic
    superphonic over 6 years
    +one for the retro Angelfire and Geocities links that gave me a huge rush of nostalgia
  • William Entriken
    William Entriken about 6 years
    Here is a much deeper example for if your email has a URL in it and that URL has stuff that needs to be encoded: stackoverflow.com/questions/4744888/…
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com about 6 years
    @tripleee what about the RFC mentioned at: stackoverflow.com/a/41365892/895245
  • tripleee
    tripleee about 6 years
    @CiroSantilli Indeed, five years later, client support is considerably more widespread, but still not entirely reliable in my experience.
  • Bram Vanroy
    Bram Vanroy over 5 years
    This link is dead.
  • Sam Carlton
    Sam Carlton over 5 years
    Here's a tool that does it all for you(Subject, Body, URL Encoding, Line Breaks) email-link-builder.netlify.com
  • bytor99999
    bytor99999 over 5 years
    Jure I really appreciate you putting this as an answer to this question as that is EXACTLY what I needed to find out and was asking myself. So while Quentin thinks this doesn't answer the OP question, it answers mine that I was searching for that brought up this post. Thank you so much Jure!
  • bytor99999
    bytor99999 over 5 years
    Can you also post an example where the body <a href> just has the href and a text between it and the end tag? Thanks
  • sed
    sed over 5 years
    <a href="<same as above>">This is the link text</a> @user1567291
  • SuperUberDuper
    SuperUberDuper almost 5 years
    is the + needed instead of spaces? works fine without on gmail
  • Ε Г И І И О
    Ε Г И І И О over 4 years
    This doesn't work. The email client will just show all the html tags as text instead of processing it as a rich text document.
  • Hassan Raza
    Hassan Raza about 4 years
    this should be without space.
  • Macumbaomuerte
    Macumbaomuerte over 3 years
    @David You cannot put a table on a subject, do you know how email subjects work? Subjects are plain-text strings. The maximum out of the basic stuff you can put on a subject is emojis
  • Peter Johnson
    Peter Johnson over 3 years
    Link is dead now
  • Vikas Lalwani
    Vikas Lalwani over 3 years
    link doesn't work, try qawithexperts.com/article/html/… To get of using details of mailto in anchor tag