Categories
JavaScript Answers

How to use JavaScript to create a client side email?

Spread the love

Sometimes, we want to use JavaScript to create a client side email.

In this article, we’ll look at how to use JavaScript to create a client side email.

How to use JavaScript to create a client side email?

To use JavaScript to create a client side email, we can create a mailto URL.

For instance, we write:

const addresses = "foo@example.com";
const body = "hello"
const subject = "subject"
const href = `mailto:${addresses}?subject=${subject}&body=${body}`
const wndMail = window.open(href, "_blank", "scrollbars=yes,resizable=yes,width=10,height=10");

to create the href mailto URL.

We combine the addresses to send to, email subject and email body into one string.

And then we call window.open with href to open the URL, which opens the default mail client with the compose email window.

Conclusion

To use JavaScript to create a client side email, we can create a mailto URL.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *