Automatically open default email client and pre-populate content

143,465

Solution 1

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

mailto:[email protected]?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:[email protected]?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.

Solution 2

JQuery:

$(function () {
      $('.SendEmail').click(function (event) {
        var email = '[email protected]';
        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>

Solution 3

Implemented this way without using Jquery:

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

sendEmail(message) {
    var email = message.emailId;
    var subject = message.subject;
    var emailBody = 'Hi '+message.from;
    document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody;
}

Solution 4

Try this: It will open the default mail directly.

<a href="mailto:[email protected]"><img src="ICON2.png"></a>
Share:
143,465
Thomas Buckley
Author by

Thomas Buckley

Updated on December 11, 2020

Comments

  • Thomas Buckley
    Thomas Buckley almost 3 years

    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?

  • Kousha
    Kousha almost 10 years
    Is there a way to load the image (not the url, but the actual image) into the message with jQuery?
  • flumingo
    flumingo about 8 years
    What if the message passes the character limit? It's a nice workaround but it doesn't seem to be an elegant solution.
  • Amay Kulkarni
    Amay Kulkarni over 7 years
    how to send html content by this way
  • UDID
    UDID over 7 years
    What if I need to add some other content as example image, css or html
  • Ajay Sharma
    Ajay Sharma almost 7 years
    Is it possible to sent attachment with the above details ?