unable to send html text using nodemailer

2019-07-20 06:38发布

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

1条回答
Animai°情兽
2楼-- · 2019-07-20 07:33

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: 'sender@gmail.com',
        to: "recipient@mail.id",
        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.
查看更多
登录 后发表回答