4

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

我的脚本是

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"],

我这里有两个问题

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

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

谢谢纳巴伦

4

5 回答 5

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()))

个性化对象

{
    personalizations: [{
        to: [
            {email: "alice@example.com"},
            {email: "bob@example.com"},
        ],
        subject: "Test Email Subject"
    }]
}

收件人无法看到对方:

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

个性化对象

{ 
    personalizations: [
        {
            to:  "alice@example.com",
            subject: "Test Email Subject"
        }, 
        { 
            to:  "bob@example.com",
            subject: "Test Email Subject"
        }
    ]
}

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

于 2019-12-04T21:39:15.763 回答
4

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%" />'
});

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

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

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

于 2013-07-04T12:56:55.263 回答
2

对于 Sendgrid 的 v3 API,我发现他们的“厨房水槽”示例很有帮助。这是其中的一个相关部分:

var helper = require('sendgrid').mail

mail = new helper.Mail()
email = new helper.Email("test@example.com", "Example User")
mail.setFrom(email)

mail.setSubject("Hello World from the SendGrid Node.js Library")

personalization = new helper.Personalization()
email = new helper.Email("test1@example.com", "Example User")
personalization.addTo(email)
email = new helper.Email("test2@example.com", "Example User")
personalization.addTo(email)

// ...

mail.addPersonalization(personalization)
于 2016-12-18T20:43:01.097 回答
2

新的 sendgrid-nodejs 更新废弃了以前的实现方法,因此现在接受的答案对您没有帮助。

所以......只是一个更新,以防有人以特定的搜索结果登陆该线程。

    to: [
      {
        email: 'email1@email.com', 
      },
      {
        email: 'email2@email.com', 
      },
    ],
于 2016-11-18T08:43:09.510 回答
1

TypeScript 的解决方案(用 ts 版本 3.4.3 和 sendGrid 7.1.1 编写),您不希望收件人能够看到对方。

import * as sendGrid from '@sendgrid/mail'

type UserEmail = {
  to: string
  subject: string
}
// Add as many recipients as you want
recipients = ['email1@global.com', 'email2@gmail.com']
const personalizations: UserEmail[] = recipients.map(admin => ({
    to: admin,
    subject: 'Inject Subject Here',
}))

try {
    await sendGrid.send({
        from, // Inject
        personalizations,
        html, // Inject
    })
} catch (err) {
    console.log(err)    
}

const personalizations看起来像这样

[{ to: 'email1@global.com',
    subject: 'Inject Subject Here' },
  { to: 'email2@global.com',
    subject: 'Inject Subject Here' }]
于 2020-06-03T10:37:44.280 回答