Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a form submission to email using node and express

How to collect data from form (name, email, attachments) and submit them to my email using express. I didn't find a comprehensive articles on this topic. I will be grateful for the help.

like image 208
K. Kudryaev Avatar asked Oct 16 '25 16:10

K. Kudryaev


1 Answers

I am considering you know how to submit form

To send any mail through node install popular module 'nodemailer'

1-install nodemailer module in your project directory

npm install nodemailer

2-come to the controller where you are handling form submit data on server

var express = require('express'),
nodemailer = require("nodemailer");

app = express.createServer();

app.use(express.bodyParser());

app.post('/formProcess', function (req, res) {
    var data=req.body;

    var smtpTransport = nodemailer.createTransport("SMTP",{
       service: "Gmail", 
       auth: {
       user: "[email protected]",
       pass: "gmailPassword"
       }});

   smtpTransport.sendMail({  //email options
   from: "Sender Name <[email protected]>",
   to: "Receiver Name <[email protected]>", // receiver
   subject: "Emailing with nodemailer", // subject
   html: "here your data goes" // body (var data which we've declared)
    }, function(error, response){  //callback
         if(error){
           console.log(error);
        }else{
           console.log("Message sent: " + res.message);
       }

   smtpTransport.close(); 
    }); });
like image 109
Anshuman Singh Avatar answered Oct 18 '25 07:10

Anshuman Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!