4

I am using Google cloud functions to send emails using Nodemailer. I am using the MailComposer module to send emails with formatting (line breaks and HTML etc).

Here is my code:

const mailOptions = {
      from: `${name} joanne@bloggs.com`,
      replyTo: `${name} ${email}`,
      to: user.email,
      bcc: 'jo@bloggs.com',
      subject: `Direct message from ${name}`,
      text: `${message}`
    }

    let mail = new MailComposer(mailOptions).compile()

    mail.keepBcc = true

    console.log('mail', mail)

    return mail.build((error, message) => {
      if (error) {
        console.log('Email unsuccessful', error)
        res.status(400).send(error)
      }
      const dataToSend = {
        to: user.email,
        bcc: 'jo@bloggs.com',
        message: message.toString('ascii')
      }
      return mailgun.messages().sendMime(dataToSend, sendError => {
        if (sendError) {
          console.log('Email unsuccessful', error)
          res.status(400).send(error)
        }
        return res.send('Email successfully sent!')
      })
    })

The problem I am having is that emails are not sent to the BBC recipient, but are sent to the TO recipient. I have tried following the documentation and adding the keepBcc option with no success. Does anyone know what I am missing?

londonfed
  • 1,170
  • 2
  • 12
  • 27

1 Answers1

0

So this was two years ago but if anyone else has the same question, here's how I solved it.

NodeMailer's MailComposer drops the BCC header when it's built. So to avoid that from happening, after you compile you must turn on the keepBcc flag. Basically, you would do this:

var mail = new MailComposer(mailOptions).compile();
mail.keepBcc = true;
mail.build(function(err, message){
  console.log(message);
}

But be sure to compile before turning on the keepBcc flag or it won't work. You can read more about why this happens here.

ruthless33
  • 21
  • 5