Search code examples
javascriptmailto

Automatically open default email client and pre-populate content


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?


Solution

  • 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.