Reputation: 6181
I am sending email using sendgrid from my app. Now I want to add cc or bcc if user reply to my mail. How Do I do this. let me explain first. I am sending answer of user's feedback comes on my web application using my application let say I am sending email via '[email protected]', and user receive this mail in his/her inbox in gmail/yahoo or any other email service. In this case user may click reply to this mail. so now, yours 'To:' has contain '[email protected]' default reply address. it's fine. Now I want to add 'cc:' (carbon copy) as follows '[email protected]'. How to do this?
Upvotes: 6
Views: 18857
Reputation: 472
If you are using version 7.6.2 of @sendgrid/mail, there is a cc
attribute that works:
import sgMail from '@sendgrid/mail'
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const msg = {
to: toAddress,
from: fromAddress, // Use the email address or domain you verified above
cc: ccAddress,
subject: `Fresh message from - ${name}`,
text: `A new message was sent by ${name} from ${ccAddress}.
${message}
`,
html: `
<p>hello world</p>
<blockquote>${message}</blockquote>
`,
}
//ES8
try {
await sgMail.send(msg)
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: 'contactSubmission function',
}),
}
} catch (error) {
console.error(error)
if (error.response) {
console.error(error.response.body)
}
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: 'error in email submission',
}),
}
}
Upvotes: 1
Reputation: 16805
For sendGrid V3 you can follow this process to add .
var sgMailHelper = require('sendgrid').mail,
sg = require('sendgrid')('apiKey');
var sender = new sgMailHelper.Email(sender, senderName||'');
var receiver = new sgMailHelper.Email(receiver);
var content = new sgMailHelper.Content("text/plain", "Test mail");
var subject = "Mail subject";
var mailObj = new sgMailHelper.Mail(sender, subject, receiver, content);
// add cc email
mailObj.personalizations[0].addCc(new sgMailHelper.Email('[email protected]'));
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mailObj.toJSON()
});
sg.API(request, function(error, response) {
if(error) {
console.log(error)
} else {
console.log('success')
}
});
Upvotes: 5
Reputation: 4964
You can pass the cc value when calling the sendgrid npm module. See below.
var sendgrid = require('sendgrid')(api_user, api_key);
var email = new sendgrid.Email({
to: '[email protected]',
from: '[email protected]',
cc: '[email protected]',
subject: 'Subject goes here',
text: 'Hello world'
});
sendgrid.send(email, function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});
Upvotes: 8