42

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?

sqluser
  • 5,502
  • 7
  • 36
  • 50

3 Answers3

51

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.

marclar
  • 3,026
  • 5
  • 35
  • 56
  • 1
    did anything change in last 10 years? Do modern browser allow sending emails by telnet-like connections to SMTP/IMAP servers? I found https://www.smtpjs.com/ but can't find any working example. I see an Email.send({}).then() function, but can't understand how to read the response value – jumpjack Feb 19 '19 at 10:15
  • No, same story - you still need an SMTP server to send mail. It looks like smtpjs.com takes you to a page where you can sign up for an email-sending service. – marclar Feb 19 '19 at 14:21
0

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" })
});
JSON C11
  • 11,272
  • 7
  • 78
  • 65
-3

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 : 'them@website.com',
    From : "you@isp.com",
    Subject : "This is the subject",
    Body : "And this is the body"}).then( message => alert(message));});
Kermit
  • 4,922
  • 4
  • 42
  • 74
Hardeep Singh
  • 818
  • 9
  • 14
  • 2
    Careful, the service that handles the script can intercept your messages, not saying it does but it's quite a risk – DeZeA Jun 06 '22 at 17:33