简体   繁体   English

如何使用 Nodemailer 以密送方式发送电子邮件

[英]How to send an email in bcc using Nodemailer

Let us say I want to send an email to recipient@xyz.com with bcc copy sent to bcc@xyz.com .假设我想发送一封电子邮件到recipient@xyz.com bcc@xyz.combcc@xyz.com密件抄送副本发送到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.bcc@xyz.com收到电子邮件时,他应该在收件人字段中看到recipient@xyz.com bcc@xyz.com ,在密件抄送字段中看到bcc@xyz.com

But when bcc@xyz.com receives the email, he is not able to see recipient@xyz.com in the To field.但是当bcc@xyz.com收到电子邮件时,他无法在recipient@xyz.com字段中看到recipient@xyz.com bcc@xyz.com

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.我也试过 cc 但 cc 没有按预期工作。

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.收到电子邮件后,我希望bcc@xyz.com收件人:字段中看到收件人@xyz.com

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. SMTP 信封通常是从消息对象中的 from、to、cc 和 bcc 字段自动生成的,但如果出于某种原因您想自己指定它(自定义信封通常用于 VERP 地址),您可以使用消息对象。

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:在您的情况下,以下 mailData 应该可以完成工作:

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'
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM