Automatically open default email client and pre-populate content
Asked Answered
C

4

119

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?

Command answered 5/11, 2012 at 11:24 Comment(2)
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.shBagman
You can follow this answer https://mcmap.net/q/188842/-compose-e-mail-via-url-params-googlemailShani
E
173

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.

Especially answered 5/11, 2012 at 11:27 Comment(5)
Is there a way to load the image (not the url, but the actual image) into the message with jQuery?Township
What if the message passes the character limit? It's a nice workaround but it doesn't seem to be an elegant solution.Factious
What if I need to add some other content as example image, css or htmlPhilous
Is it possible to sent attachment with the above details ?Rosamondrosamund
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 serviceStroke
D
21

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>
Darbee answered 24/2, 2016 at 11:48 Comment(1)
how to send html content by this wayMeneses
P
6

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;
}
Porterfield answered 18/1, 2017 at 17:9 Comment(0)
T
4

Try this: It will open the default mail directly.

<a href="mailto:[email protected]"><img src="ICON2.png"></a>
Trilobate answered 5/8, 2017 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.