5

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

swathi anupuram
  • 795
  • 1
  • 7
  • 14

1 Answers1

4

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.
jerry
  • 745
  • 6
  • 20
  • `html` in `sendMail` arguments is the key here. – ivosh Jun 04 '19 at 10:01
  • 2
    @ivosh I can't find any documentation on the requirement of `html` and `text` field in the `sendMail` function. Does nodeMailer automatically determine what kind of email to send or are they both being sent and rendered by the end user mail client? – VladNeacsu Apr 28 '20 at 11:16
  • I ran into this too. the {{ body }} replacement shows raw markup encoded or decoded. – Mark Mar 16 '21 at 16:20
  • hint: solved this by just doing a `emailTemplate = emailTemplate.replace('{{body}}', body);`. That old template system was just injecting plain text. – Mark Mar 16 '21 at 16:38