anirudha gupta
anirudha gupta

Reputation:

How to send email by using javascript or jquery

I developed a web-form for a blog, and I need to send its values to an email.

How can I send an email by using jQuery or JavaScript alone?

Upvotes: 42

Views: 175089

Answers (3)

Hardeep Singh
Hardeep Singh

Reputation: 866

You can send mail by Jquery just follow these steps

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

After that use this code:

    $( document ).ready(function() {
     Email.send({
    Host : "smtp.yourisp.com",
    Username : "username",
    Password : "password",
    To : '[email protected]',
    From : "[email protected]",
    Subject : "This is the subject",
    Body : "And this is the body"}).then( message => alert(message));});

Upvotes: -3

JSON C11
JSON C11

Reputation: 11792

You can do it server-side with nodejs.

Check out the popular Nodemailer package. There are plenty of transports and plugins for integrating with services like AWS SES and SendGrid!

The following example uses SES transport (Amazon SES):

let nodemailer = require("nodemailer");
let aws = require("aws-sdk");
let transporter = nodemailer.createTransport({
  SES: new aws.SES({ apiVersion: "2010-12-01" })
});

Upvotes: 0

marclar
marclar

Reputation: 3046

The short answer is that you can't do it using JavaScript alone. You'd need a server-side handler to connect with the SMTP server to actually send the mail. There are many simple mail scripts online, such as this one for PHP:

Simple PHP mail script

Using a script like that, you'd POST the contents of your web form to the script, using a function like this:

jQuery.post

And then the script would take those values, plus a username and password for the mail server, and connect to the server to send the mail.

Upvotes: 51

Related Questions