NodeJS Sendgrid 向多个收件人发送电子邮件的问题

Nab*_*abs 4 node.js sendgrid

我在向多个收件人发送邮件时遇到问题。

我的脚本是

var SendGrid = require('sendgrid').SendGrid;
var sendgrid = new SendGrid('<<username>>', '<<password>>');      
    sendgrid.send({
    to: 'nabababa@gmail.com',   
from: 'sengupta.nabarun@gmail.com',
bcc: ["sengupta.nabarun@gmail.com","sengupta_nabarun@rediffmail.com"],
Run Code Online (Sandbox Code Playgroud)

我在这里有两个问题

  1. 我可以列出一组收件人吗?
  2. 如何在密件抄送列表中获取一组收件人?

与上述两个查询相关的解决方案确实会有所帮助

谢谢纳巴伦

Nic*_* Q. 6

to您可以在和字段中使用收件人数组bcc

例如:

var SendGrid = require('sendgrid').SendGrid;
var sendgrid = new SendGrid('{{sendgrid username}}', '{{sendgrid password}}');      
sendgrid.send({
    to: ['one@example.com', 'two@example.com'],
    from: 'nick@sendgrid.com',
    bcc: ['three@example.com', 'four@example.com'],
    subject: 'This is a demonstration of SendGrid sending email to mulitple recipients.',
    html: '<img src="http://3.bp.blogspot.com/-P6jNF5dU_UI/TTgpp3K4vSI/AAAAAAAAD2I/V4JC33e6sPM/s1600/happy2.jpg" style="width: 100%" />'
});
Run Code Online (Sandbox Code Playgroud)

如果这对您不起作用并且 Node 没有输出任何错误,请通过登录 SendGrid 的网站并查看电子邮件活动日志来检查电子邮件是否正在发送。

我在测试代码示例时遇到的一件事是,如果您将 和 发送tobcc同一个 gmail 地址,gmail 会将其全部合并到一封电子邮件中(因此看起来它不起作用)。确保测试时将电子邮件发送到完全不同的帐户。

如果您需要一些电子邮件帐户来测试,Guerrilla Mail是创建临时测试帐户的绝佳选择。


fob*_*oba 5

这是我最终得到的解决方案,并认为它更简单,可能对人们有所帮助。

请注意个性化对象形状的不同。

收件人可以看到对方:


const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)

// Declare the content we'll use for the email
const FROM_EMAIL = 'example@example.io' // <-- Replace with your email
const subject = 'Test Email Subject'
const body = '<p>Hello HTML world!</p>'
const recipients = ['alice@example.com', 'bob@example.com'] // <-- Add your email(s) here to test

// Create the personalizations object that will be passed to our message object
let personalizations = [{
    to: [],
    subject
}]

// Iterate over our recipients and add them to the personalizations object
for (let index in recipients) {
    personalizations[0].to[index] = { email: recipients[index] }
}

const msg = {
    personalizations,
    from: FROM_EMAIL,
    html: body,
}

// Log to see what our message object looks like
console.log(msg)

// Send the email, if success log it, else log the error message
sgMail.send(msg)
    .then(() => console.log('Mail sent successfully'))
    .catch(error => console.error(error.toString()))
Run Code Online (Sandbox Code Playgroud)

个性化对象

{
    personalizations: [{
        to: [
            {email: "alice@example.com"},
            {email: "bob@example.com"},
        ],
        subject: "Test Email Subject"
    }]
}
Run Code Online (Sandbox Code Playgroud)

收件人无法看到对方:

// Create the personalizations object that will be passed to our message object
personalizations = []

// Iterate over our recipients and add them to the personalizations object
for (let index in recipients) {
    personalizations[index] = { to: recipients[index], subject}
}
Run Code Online (Sandbox Code Playgroud)

个性化对象

{ 
    personalizations: [
        {
            to:  "alice@example.com",
            subject: "Test Email Subject"
        }, 
        { 
            to:  "bob@example.com",
            subject: "Test Email Subject"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个包含完整解决方案的RunKit,您可以在其中进行测试。