我在向多个收件人发送邮件时遇到问题。
我的脚本是
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"],
我这里有两个问题
与上述两个查询相关的解决方案确实很有帮助
由于 Nabarun
答案 0 :(得分:3)
对于Sendgrid的v3 API,我发现他们的"kitchen sink" example很有帮助。这是相关的一点:
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)
答案 1 :(得分:2)
您可以在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的网站并查看{{查看电子邮件是否正在发送。 3}}
我在测试您的代码示例时遇到的一件事是,如果您将to
和bcc
发送到同一个gmail地址,gmail会将它们全部合并到一封电子邮件中(所以它看来它没有用)。确保在测试时您将电子邮件发送到完全不同的帐户。
如果您需要使用Email Activity Log进行测试的某些电子邮件帐户是创建临时测试帐户的绝佳选择。
答案 2 :(得分:1)
新的sendgrid-nodejs更新已废弃以前的实现方法,因此接受的答案现在无法帮助您。
所以...只是一个更新,以防任何人使用特定搜索结果登陆此主题。
to: [
{
email: 'email1@email.com',
},
{
email: 'email2@email.com',
},
],
答案 3 :(得分:0)
这是我最终得出的解决方案,并认为它更直接,可能对人们有所帮助。
请注意个性化对象形状的差异。
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"
}
]
}
我创建了一个带有完整解决方案的run kit,您可以在此处进行测试。
答案 4 :(得分:0)
一种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' }]