8

Let us say I want to send an email to recipient@xyz.com with bcc copy sent to bcc@xyz.com.

When bcc@xyz.com is receiving an email he should see recipient@xyz.com in the To field, and bcc@xyz.com in the bcc field.

But when bcc@xyz.com receives the email, he is not able to see recipient@xyz.com in the To field.

I tried to create and send email using mail composer instead of transports but it is not working as expected.

I also tried cc but cc is not working as expected.

const nodemailer = require('nodemailer');
const testAccount = await nodemailer.createTestAccount();
const transporter = nodemailer.createTransport({
    host: "smtp.ethereal.email",
    auth: {
        user: testAccount.user,
        pass: testAccount.pass
    },
    tls: { rejectUnauthorized: false }
});

const mailData = {
    from: 'xyz@xyz.com',
    to: 'recipient@xyz.com',
    bcc: 'bcc@xyz.com',
    subject: 'Sample Mail',
    html: text
}

const result = await transporter.sendMail(mailData);

console.log('Mail Sent! \t ID: ' + result.messageId);

When the email is received, I expect bcc@xyz.com to see recipient@xyz.com in the To: field.

cssyphus
  • 37,875
  • 18
  • 96
  • 111
Soham Lawar
  • 1,165
  • 1
  • 12
  • 20
  • 1
    Can't get this question, because I'm having this behaviour by default when sending e-mail, without the need to do anything with `envelope`. – Daniel Danielecki Dec 02 '20 at 20:20

1 Answers1

17

See envelope

SMTP envelope is usually auto generated from from, to, cc and bcc fields in the message object but if for some reason you want to specify it yourself (custom envelopes are usually used for VERP addresses), you can do it with the envelope property in the message object.

let message = {
  ...,
  from: 'mailer@nodemailer.com', // listed in rfc822 message header
  to: 'daemon@nodemailer.com', // listed in rfc822 message header
  envelope: {
    from: 'Daemon <deamon@nodemailer.com>', // used as MAIL FROM: address for SMTP
    to: 'mailer@nodemailer.com, Mailer <mailer2@nodemailer.com>' // used as RCPT TO: address for SMTP
  }
}

In your case following mailData should do the work:

const mailData = {
    from: 'xyz@xyz.com',
    to: 'recipient@xyz.com',
    bcc: 'bcc@xyz.com',
    subject: 'Sample Mail',
    html: text,
    envelope: {
        from: 'xyz@xyz.com',
        to: 'recipient@xyz.com'
    }
}
Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
Jakub Jindra
  • 551
  • 6
  • 12