简体   繁体   English

发送网格:在email中添加cc

[英]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.

相关问题 有没有办法在SendGrid的Automation Email系统中添加一个CC email地址? - Is there a way to add a CC email address in SendGrid's Automation Email system? 如何在 .NET Core 3.1 和 C# 中使用 SendGrid 在发送 Email 中添加抄送或密送 - How to Add CC or Bcc in Send Email With SendGrid in .NET Core 3.1 and C# 如何将 email 添加到 Sendgrid 的自动化中? - How does one add an email to an automation in Sendgrid? nodejs中如何在sendgrid中添加好email api - How to add a good email in sendgrid api in nodejs Sendgrid - 通过动态模板添加 email 附件 - Sendgrid - add email attachments via dynamic templates 在 Azure Runbook 中添加 CC 以发送 email SendGrid JSON 时出现 400 错误请求 - 400 Bad Request when adding CC for send email SendGrid JSON in Azure Runbook 使用 sendgrid 在 email 中嵌入图像 - Embedded images in email with sendgrid Sendgrid - Email 格式化 - Sendgrid - Email Formatting 如何将 JSON 部分标签添加到 SendGrid 联系人,以便在动态 email 中使用车把模板语言进行解析 - How to add JSON section tags to a SendGrid contact for parsing with handlebars templating language in dynamic email 如何使用模板发送 Sendgrid email 并为模板添加自定义数据? - How do I send a Sendgrid email using a template and add custom data for the template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM