Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

node.js - NodeJS Sendgrid Issue in sending email to multiple recipients

I am having issue in sending mails to multiple recipients.

My script is

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

I have two questions here

  1. Can I have an array of recipients in to list?
  2. How can I get an array of recipients in bcc list?

Solutions related to above two queries will be indeed helpful

Thanks Nabarun

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the solution that I ended up with and thought it was more straightforward and could be helpful for folks.

Note the difference in the shape of the personalizations object.

Recipients can see each other:


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 Object:

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

Recipients can not see each other:

// 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 Object:

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

I created a RunKit with the full solution and where you can test it out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...