5

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 'noreply@mydomain.com', 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 'noreply@mydomain.com' default reply address. it's fine. Now I want to add 'cc:' (carbon copy) as follows 'feedback@mydomain.com'. How to do this?

Ajoy
  • 1,838
  • 3
  • 30
  • 57
Manish Sapkal
  • 5,591
  • 8
  • 45
  • 74

3 Answers3

8

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:       '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);
});
Jeff Sloyer
  • 4,899
  • 1
  • 24
  • 48
5

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('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')
        }
      });
dbburgess
  • 751
  • 6
  • 18
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
1

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',
      }),
    }
  }

brianbancroft
  • 463
  • 3
  • 17