954

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

Andrew Brēza
  • 5,779
  • 2
  • 30
  • 39
Jiew Meng
  • 74,635
  • 166
  • 442
  • 756

13 Answers13

1565

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

mailto subject example:

<a href="mailto:no-one@snai1mai1.com?subject=free chocolate">example</a>

mailto with content:

<a href="mailto:no-one@snai1mai1.com?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
Esko
  • 3,725
  • 2
  • 17
  • 34
Haim Evgi
  • 114,996
  • 43
  • 205
  • 218
129
<a href="mailto:manish@simplygraphix.com?subject=Feedback for 
webdevelopersnotes.com&body=The Tips and Tricks section is great
&cc=anotheremailaddress@anotherdomain.com
&bcc=onemore@anotherdomain.com">Send me an email</a>

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

45

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:infobot@example.com?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:infobot@example.com?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>";
?>
William Entriken
  • 30,701
  • 17
  • 128
  • 168
  • 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: https://stackoverflow.com/questions/4744888/how-to-properly-url-encode-a-string-in-php/48267785#48267785 – William Entriken Jan 18 '18 at 15:57
30

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

Dawson B
  • 661
  • 9
  • 13
26

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:test@example.com?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:test@example.com?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:test@example.com?subject=testing out mailto&body=Just testing&cc=test1@example.com&bcc=test1@example.com">Third
    Example</a>

How to add subject to mailto tag

niksmac
  • 2,190
  • 3
  • 26
  • 44
13
mailto:joe@company.com?subject=Your+subject
payne
  • 12,793
  • 5
  • 35
  • 45
  • 1
    My `subject` is not text. It is a table. How to add that. – David Jul 08 '16 at 04:26
  • @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 – Macumbaomuerte Oct 10 '20 at 11:57
9

I split it into separate lines to make it a little more readable.

<a href="

    mailto:johndoe@gmail.com

    ?subject=My+great+email+to+you

    &body=This+is+an+awesome+email

    &cc=janedoe@gmail.com

    &bcc=billybob@yahoo.com

">Click here to send email!</a>
quemeful
  • 8,148
  • 4
  • 51
  • 64
7

here is the trick http://neworganizing.com/content/blog/tip-prepopulate-mailto-links-with-subject-body-text

<a href="mailto:tips@neworganizing.com?subject=Your+tip+on+mailto+links&body=Thanks+for+this+tip">tell a friend</a>
Arun Kushwaha
  • 894
  • 1
  • 7
  • 8
7

Yes:

Use this to experiment with mailto form elements and link encoding.

You can enter subject, body (i.e. content), etc. into the form, hit the button and see the mailto html link that you can paste into your page.

You can even specify elements that are rarely known and used: cc, bcc, from emails.

NEER
  • 3,932
  • 5
  • 17
  • 34
woz
  • 197
  • 3
  • 10
  • 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 Aug 28 '15 at 15:50
  • I mean, if you choose to encode all characters, they are all converted to HTML notation `` (avoiding some spam bots). – Armfoot Aug 28 '15 at 15:58
  • 4
    This link is dead. – Bram Vanroy Sep 05 '18 at 12:37
5

Note that it is not possible to use HTML in the message body, according to RFC 2368:

The special hname "body" indicates that the associated hvalue is the body of the message. The "body" hname should contain the content for the first text/plain body part of the message. The mailto URL is primarily intended for generation of short text messages that are actually the content of automatic processing (such as "subscribe" messages for mailing lists), not general MIME bodies.

Credit: https://stackoverflow.com/a/13415988/1835519

Community
  • 1
  • 1
Jure Sah
  • 159
  • 2
  • 4
  • 2
    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. – Quentin Aug 10 '16 at 19:25
  • 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! – user1567291 Dec 14 '18 at 17:53
5

Yes, you can like this:

mailto: email@host.com?subject=something
MeanEYE
  • 971
  • 7
  • 24
3

Here's a runnable snippet to help you generate mailto: links with optional subject and body.

function generate() {
  var email = $('#email').val();
  var subject = $('#subject').val();
  var body = $('#body').val();

  var mailto = 'mailto:' + email;
  var params = {};
  if (subject) {
    params.subject = subject;
  }
  if (body) {
    params.body = body;
  }
  if (params) {
    mailto += '?' + $.param(params);
  }

  var $output = $('#output');
  $output.val(mailto);
  $output.focus();
  $output.select();
  document.execCommand('copy');
}

$(document).ready(function() {
  $('#generate').on('click', generate);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="email" placeholder="email address" /><br/>
<input type="text" id="subject" placeholder="Subject" /><br/>
<textarea id="body" placeholder="Body"></textarea><br/>
<button type="button" id="generate">Generate & copy to clipboard</button><br/>
<textarea id="output">Output</textarea>
congusbongus
  • 10,870
  • 5
  • 64
  • 86
1

If you want to add html content to your email, url encode your html code for the message body and include it in your mailto link code, but trouble is you can't set the type of the email from this link from plaintext to html, the client using the link needs their mail client to send html emails by default. In case you want to test here is the code for a simple mailto link, with an image wrapped in a link (angular style urls added for visibility):

<a href="mailto:?body=%3Ca%20href%3D%22{{ scope.url }}%22%3E%3Cimg%20src%3D%22{{ scope.url }}%22%20width%3D%22300%22%20%2F%3E%3C%2Fa%3E">

The html tags are url encoded.

serdarsenay
  • 2,363
  • 19
  • 21