[英]sendgrid : add cc in email
I am sending email using sendgrid from my app.我从我的应用程序使用 sendgrid 发送 email。 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 'noreply@mydomain.com', and user receive this mail in his/her inbox in gmail/yahoo or any other email service.我正在发送用户反馈的答复来自我的 web 应用程序使用我的应用程序假设我正在通过'noreply@mydomain.com'发送 email,并且用户在他/她的 gmail/yahoo 收件箱或任何其他 email 服务中收到此邮件。 In this case user may click reply to this mail.在这种情况下,用户可以单击回复此邮件。 so now, yours 'To:' has contain 'noreply@mydomain.com' default reply address.所以现在,您的“收件人:”已包含“noreply@mydomain.com”默认回复地址。 it's fine.没关系。 Now I want to add 'cc:' (carbon copy) as follows 'feedback@mydomain.com'.现在我想添加“cc:”(抄送),如下所示“feedback@mydomain.com”。 How to do this?这该怎么做?
You can pass the cc value when calling the sendgrid npm module. 您可以在调用sendgrid npm模块时传递cc值。 See below. 见下文。
var sendgrid = require('sendgrid')(api_user, api_key);
var email = new sendgrid.Email({
to: 'foo@bar.com',
from: 'you@yourself.com',
cc: 'someone@else.com',
subject: 'Subject goes here',
text: 'Hello world'
});
sendgrid.send(email, function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});
For sendGrid V3 you can follow this process to add . 对于sendGrid V3,您可以按照此过程添加。
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('cc.email@gmail.com'));
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')
}
});
If you are using version 7.6.2 of @sendgrid/mail, there is a cc
attribute that works:如果您使用的是@sendgrid/mail 的 7.6.2 版,则有一个有效的cc
属性:
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',
}),
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.