Sometimes, we want to automatically open default email client and pre-populate content with JavaScript.
In this article, we’ll look at how to automatically open default email client and pre-populate content with JavaScript.
How to automatically open default email client and pre-populate content with JavaScript?
To automatically open default email client and pre-populate content with JavaScript, we can navigate to the mailto
URL with the email subject and body filled in.
For instance, we write
const email = message.emailId;
const subject = message.subject;
const emailBody = "Hi " + message.from;
document.location =
"mailto:" + email + "?subject=" + subject + "&body=" + emailBody;
to set document.location
to a mailto
URL string with the subject
and body
of the email.
This will make the browser open the default mail client with the subject and body of the email filled in with the email
address.
Conclusion
To automatically open default email client and pre-populate content with JavaScript, we can navigate to the mailto
URL with the email subject and body filled in.