Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to send html text using nodemailer

Tags:

I am unable to send html text in mail using nodemailer.

exports.send = function(req, res) {
console.log(req.query);
var mailOptions = {
    to: req.query.email,
    subject: req.query.sub,
    text: 'Date of Interview: ' + req.query.dateOfInterview+ 'Time of Interview: ' + req.query.timeOfInterview + '' + req.query.assignedTechnicalPerson + '' + req.query.typeOfInterview + '' + req.query.interviewLocation
}
smtpTransport.sendMail(mailOptions, function(error, response) {
    if (error) {
        console.log(error);
        res.end("error");
    } else {
        console.log("Message sent: " + response.message);
        res.end("sent");

    }
});

};

I am getting mail as continuous text without any line space How can i send the same text using html tags in it i have also tried keeping html and end up getting lots of errors

Please say me correct syntax

Any help is appreciated

like image 205
swathi anupuram Avatar asked Jul 19 '16 06:07

swathi anupuram


1 Answers

Here is the working code with nodemailer latest version.

    var smtpTransport = require('nodemailer-smtp-transport');
    var transporter = nodeMailer.createTransport(
        smtpTransport({
            service: 'gmail',
            auth: {
                user: <Your gmail>,
                pass: '*****'//ur password
            }
        })
    );
    transporter.sendMail({
        from: '[email protected]',
        to: "[email protected]",
        subject: 'hello world!',
        //text:"one"
        html: '<html><body>Hello World....</body></html>'
    }, function(error, response) {
        if (error) {
            console.log(error);
        } else {
            console.log('Message sent');
        }
    });

Note: To give access for smtp do the following:

  1. For Gmail you may need to configure "Allow Less Secure Apps" in your Gmail account. Click here
  2. You also may need to unlock your account with "Allow access to your Google account" to use SMTP.
  3. If you are using 2FA in that case you would have to create an Application specific password.
like image 121
jerry Avatar answered Oct 06 '22 00:10

jerry