112

I need to automatically open a user's default email client when they save some content on a page. I need to populate the email subject, to address, and put some content in the email body.

What is the best option to achieve this?

I'm aware of the mailto: attribute, but the user must click on this and I'm not sure it allows you to specifiy the subject and content?

Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69
Thomas Buckley
  • 5,836
  • 19
  • 62
  • 110
  • You can set each and every part of an email in a mailto-prefixed href. Here's a tool I built to make it dead simple: mailto.now.sh – Dawson B Nov 08 '17 at 23:08

4 Answers4

165

As described by RFC 6068, mailto allows you to specify subject and body, as well as cc fields. For example:

mailto:username@example.com?subject=Subject&body=message%20goes%20here

User doesn't need to click a link if you force it to be opened with JavaScript

window.location.href = "mailto:user@example.com?subject=Subject&body=message%20goes%20here";

Be aware that there is no single, standard way in which browsers/email clients handle mailto links (e.g. subject and body fields may be discarded without a warning). Also there is a risk that popup and ad blockers, anti-virus software etc. may silently block forced opening of mailto links.

Community
  • 1
  • 1
jsalonen
  • 29,593
  • 15
  • 91
  • 109
  • 1
    Is there a way to load the image (not the url, but the actual image) into the message with jQuery? – Kousha Jan 28 '14 at 18:26
  • 10
    What if the message passes the character limit? It's a nice workaround but it doesn't seem to be an elegant solution. – flumingo Sep 15 '15 at 14:33
  • 2
    What if I need to add some other content as example image, css or html – UDID Jul 11 '16 at 07:33
  • 1
    Is it possible to sent attachment with the above details ? – Ajay Sharma Feb 02 '17 at 12:30
  • If it passes character limit, you could use encodeURIComponent(body). But still the length of the email body may still be limited by the email client or the recipient's email service – ullas kakanadan Mar 10 '23 at 10:15
19

JQuery:

$(function () {
      $('.SendEmail').click(function (event) {
        var email = 'sample@gmail.com';
        var subject = 'Test';
        var emailBody = 'Hi Sample,';
        var attach = 'path';
        document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody+
            "?attach="+attach;
      });
    });

HTML:

 <button class="SendEmail">Send Email</button>
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
user5506072
  • 191
  • 1
  • 2
5

Implemented this way without using Jquery:

<button class="emailReplyButton" onClick="sendEmail(message)">Reply</button>

function sendEmail(message) {
    var email = message.emailId;
    var subject = message.subject;
    var emailBody = 'Hi '+message.from;
    document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody;
}
answerSeeker
  • 2,692
  • 4
  • 38
  • 76
3

Try this: It will open the default mail directly.

<a href="mailto:demo@demo.com"><img src="ICON2.png"></a>
Arun J
  • 687
  • 4
  • 14
  • 27
subindas pm
  • 2,668
  • 25
  • 18