Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bcc headers are stripped in MailComposer #1342

Closed
danielepolencic opened this issue Oct 22, 2021 · 8 comments
Closed

Bcc headers are stripped in MailComposer #1342

danielepolencic opened this issue Oct 22, 2021 · 8 comments

Comments

@danielepolencic
Copy link

This issue is related to MailComposer and MimeNode.

With MailComposer I can create an email with a Bcc header like this:

const mailcomposer = new MailComposer({
          to,
          cc,
          bcc: ['email@inbcc.com'],
          html: body,
          subject,
          attachments: [],
        })

However, when the email is built, the Bcc is removed:

const payload = await mailcomposer.compile().build()
console.log(payload.toString('ascii'))
// Bcc header is missing in the output

Digging in the code reveals that the Bcc header is removed by MimeNode unless you set keepBcc: true in the constructor.

* @param {Boolean} [options.keepBcc] If true, do not exclude Bcc from the generated headers

Unfortunately, there is no way to pass an argument to MailComposer that cascades to MimeNode (at least from my initial investigation).

I can think about two options to make this work:

  1. If the bcc is set in MailComposer, make sure that keepBcc is true in MimeNode.
  2. Expose the config for MimeNode in MailComposer.

I don't fully grasp the implications of 2) and I suspect it's not straightforward. If 1) is doable, I can raise a PR along the lines of

in

if (!parentNode) {
node = new MimeNode(element.contentType, {
filename: element.filename,
baseBoundary: this.mail.baseBoundary,
textEncoding: this.mail.textEncoding,
boundaryPrefix: this.mail.boundaryPrefix,
disableUrlAccess: this.mail.disableUrlAccess,
disableFileAccess: this.mail.disableFileAccess,
normalizeHeaderKey: this.mail.normalizeHeaderKey,
newline: this.mail.newline
});
} else {
node = parentNode.createChild(element.contentType, {
filename: element.filename,
textEncoding: this.mail.textEncoding,
disableUrlAccess: this.mail.disableUrlAccess,
disableFileAccess: this.mail.disableFileAccess,
normalizeHeaderKey: this.mail.normalizeHeaderKey,
newline: this.mail.newline
});
}

add this one liner:

node = new MimeNode(element.contentType, {
                filename: element.filename,
                baseBoundary: this.mail.baseBoundary,
                textEncoding: this.mail.textEncoding,
                boundaryPrefix: this.mail.boundaryPrefix,
                disableUrlAccess: this.mail.disableUrlAccess,
                disableFileAccess: this.mail.disableFileAccess,
                normalizeHeaderKey: this.mail.normalizeHeaderKey,
                newline: this.mail.newline
                keepBcc: !!this.mail.bcc // <-- this line
            });

I believe the same change has to be extended to the next block too.

@stale
Copy link

stale bot commented Jan 9, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 9, 2022
@jkozieronek
Copy link

I can think about two options to make this work:

  1. If the bcc is set in MailComposer, make sure that keepBcc is true in MimeNode.
  2. Expose the config for MimeNode in MailComposer.

Option 1 is not backwards compatible.
Option 2 is the correct fix. MainComposer.compile() can take optional MineNode options.

Luckily, compile() returns a MimeNode which has the property keepBcc (it's just not exposed in TS types) so you can set it before calling build. See https://nodemailer.com/extras/mailcomposer/#bcc

var mailOptions = {
   ...
   bcc: 'bcc@example.com'
}

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

If using TypeScript, just add // @ts-ignore above the offending line until types are fixed.

@danielepolencic
Copy link
Author

I can confirm that the code above fixes it. Thanks!

@gojevicmario
Copy link

I had the same issue today, @danielepolencic can you reopen this issue?
The solution above is more of a workaround than an actual solution. I believe this was just an oversight when creating the type definitions and shouldn't be a big deal to fix.

@stale stale bot removed the wontfix label Oct 11, 2023
@andris9
Copy link
Member

andris9 commented Oct 22, 2023

The Bcc field usage has been documented here. I agree that this is counterintuitive and bad API design, but it has worked like this for 10+ years already, and I'm not really up to changing Nodemailer's API anymore.

@AnandChowdhary
Copy link

The solution above is more of a workaround than an actual solution. I believe this was just an oversight when creating the type definitions and shouldn't be a big deal to fix.

@andris9 I could be wrong, but I think @gojevicmario meant that the type definition in @types/nodemailer does not allow keepBcc:

CleanShot 2024-04-22 at 09 16 54@2x

@andris9
Copy link
Member

andris9 commented Apr 22, 2024

@AnandChowdhary the typings for Nodemailer are community-maintained. I have no means to modify these. You would have to connect to the persons maintaining the typings file and ask them to update it.

@AnandChowdhary
Copy link

Thank you! I have opened a discussion here: DefinitelyTyped/DefinitelyTyped#69409

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants