The Wayback Machine - https://web.archive.org/web/20240528171220/https://www.geeksforgeeks.org/how-to-send-an-email-from-javascript/
Open In App

How to send an email from JavaScript ?

Last Updated : 20 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to send mail using Simple Mail Transfer Protocol which is a free JavaScript library. It is basically used to send emails, so it only works for outgoing emails. To be able to send emails, you need to provide the correct SMTP server when you set up your email client. Most internet systems use SMTP as a method to transfer mail from one user to another. It is a push protocol. In order to use SMTP, you need to configure your Gmail. You need to change two settings of your Gmail account from which you are sending the mail i.e.

  • Revoke 2-step verification
  • Enabling less secure apps to access Gmail. You can easily do this by clicking on the link Enable

After this just create an HTML file and include SMTP in your <script></script> tag : 

<script src="https://smtpjs.com/v3/smtp.js"></script>

Example: Below is the HTML file which you will need to run in order to send the mail. 

html




<!DOCTYPE html>
<html>
<head>
    <title>Send Mail</title>
    <script src="https://smtpjs.com/v3/smtp.js">
    </script>
 
    <script type="text/javascript">
        function sendEmail() {
            Email.send({
                Host: "smtp.gmail.com",
                Username: "sender@email_address.com",
                Password: "Enter your password",
                To: 'receiver@email_address.com',
                From: "sender@email_address.com",
                Subject: "Sending Email using javascript",
                Body: "Well that was easy!!",
            })
                .then(function (message) {
                    alert("mail sent successfully")
                });
        }
    </script>
</head>
 
<body>
    <form method="post">
        <input type="button" value="Send Email"
               onclick="sendEmail()" />
    </form>
</body>
</html>


Output: Just click on the button and the mail will be sent: You will see the below pop-up if the mail has been sent successfully.

How to send an email from JavaScript ?

How to send an email from JavaScript ?

Now the question is what if you have multiple receivers? In that case, you have to do nothing just configure your sendMail() function as described below:

to: 'first_username@gmail.com, second_username@gmail.com',

Rest all will be same. If you want to send HTML formatted text to the receiver, then you need to add the below code in your mail function:

html: "<h1>GeeksforGeeks</h1>
<p>A computer science portal</p>"

At last, in order to send an attachment just write the following code in sendMail() function:

Attachments : [{
    name : "File_Name_with_Extension",
    path:"Full Path of the file"
}]

Example: So the final JavaScript code after the above configuration will look like as follows:

html




<!DOCTYPE html>
<html>
<head>
    <title>Sending Mail</title>
    <script src="https://smtpjs.com/v3/smtp.js"></script>
    <script type="text/javascript">
        function sendEmail() {
            Email.send({
                Host: "smtp.gmail.com",
                Username: "sender@email_address.com",
                Password: "Enter your password",
                To: 'receiver@email_address.com',
                From: "sender@email_address.com",
                Subject: "Sending Email using javascript",
                Body: "Well that was easy!!",
                Attachments: [
                    {
                        name: "File_Name_with_Extension",
                        path: "Full Path of the file"
                    }]
            })
                .then(function (message) {
                    alert("Mail has been sent successfully")
                });
        }
    </script>
</head>
 
<body>
    <form method="post">
        <input type="button" value="Send Mail"
               onclick="sendEmail()" />
    </form>
</body>
</html>


Output:

How to send an email from JavaScript ?

How to send an email from JavaScript ?

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

HTML is the foundation of webpages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.



Similar Reads

PHP | Send Attachment With Email
Sending an email is a very common activity in a web browser. For example, sending an email when a new user joins a network, sending a newsletter, sending greeting mail, or sending an invoice. We can use the built-in mail() function to send an email programmatically. This function needs three required arguments that hold the information about the re
7 min read
How to send an email using PHPMailer ?
PHPMailer is a code library and used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming. PHPMailer simplifies the process of sending emails and it is very easy to us
3 min read
How to Send Email using Mailgun API in Node.js ?
Sending an email is an essential part of any project and it can be achieved by using Mailgun API. It is very popular for sending emails. Features of Mailgun: . It is easy to get started and easy to use.It is a widely used and popular module for sending emails.Mails can also be scheduled. Installation of Mailgun module: You can visit the link to Ins
2 min read
How to send Attachments and Email using nodemailer in Node.js ?
For this purpose, we will use a package called nodemailer. It is a module that makes email sending pretty easy. For using it, you will need to install by using the following command: $ npm install nodemailer Features of nodemailer module: It has zero dependencies and heavy security.You can use it hassle-free whether it is Azure or Windows box.It al
2 min read
How to send email using Node.js ?
Electronic Mail is a widely used method to transfer/exchange messages among people. We generally send the email with any software or application like Gmail, outlook, thunderbird mail, and yahoo, etc. We can also code the basic concept of these applications in a node.js app by using any third-party library which can interact with the networking syst
7 min read
How to create a text that permit to send email in HTML document when button is clicked ?
In this article, we will learn How to create the text that permits sending the email in the HTML document when the button is clicked. We can simply create a form that can be used for creating the text i.e body of the message as well as it can be used for sending the email to the respective address. The action property of the form is used to specify
2 min read
How to send email verification link with firebase using ReactJS?
Email verification is a crucial step in user authentication and account security. It ensures that users provide a valid email address and allows you to verify their identity before granting them access to certain features or functionalities. Firebase, a powerful platform for building web and mobile applications, provides a simple and effective way
3 min read
How to send email with Nodemailer using Gmail account in Node ?
Nodemailer is the Node.js npm module that allows you to send emails easily. In this article, we will cover each step to send email using a Gmail account with the help of nodemailer.  Prerequisites:NPM and NodeJSExpressJSApproach to send email with Nodemailer:Include the nodemailer module in the code using require('nodemailer').Use nodemailer.create
3 min read
How to Create a Link to Send Email in HTML ?
Incorporating email links into HTML pages facilitates a smooth way for users to send a message directly from the webpage. We will see how to create these email links, making your web pages user-friendly and interactive. Table of Content Using Mailto Protocol with Predefined SubjectCreating Link to send Email with predefined CC and BCC Using Mailto
2 min read
How to send a test Email from the WordPress ?
Sending test emails from your WordPress website is crucial to ensure reliable communication with your users. Whether it’s password reset emails, order confirmations, or notifications, you want to be certain that your email system works flawlessly. WordPress users often face the problem of sending emails to their customers through the WordPress webs
4 min read