Send emails using Sendgrid with google appscript

一世执手 提交于 2019-12-03 04:58:18

Try the below code. It worked for me

   var SENDGRID_KEY ='Your API KEY';

  var headers = {
    "Authorization" : "Bearer "+SENDGRID_KEY, 
    "Content-Type": "application/json" 
  }

  var body =
  {
  "personalizations": [
    {
      "to": [
        {
          "email": "email id of the sender"
        }
      ],
      "subject": "Hello, World!"
    }
  ],
  "from": {
    "email": "From email id"
  },
  "content": [
    {
      "type": "text",
      "value": "Hello, World!"
    }
  ]
}

  var options = {

    'method':'post',
    'headers':headers,
    'payload':JSON.stringify(body)


  }


 var response = UrlFetchApp.fetch("https://api.sendgrid.com/v3/mail/send",options);


 Logger.log(response); 

Also ensure that the API key you created in SendGrid has the all the credentials it needs to send the email

For anyone who has this issue in the future with Transactional Email Template:

https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/

This is the function to send (similar to the answer of Nikil Mathew, but for transactional email template with dynamic data):

export const sendBySendGrid = (toEmail, templateId, dynamicTemplateData) => {
  const headers = {
    Authorization: `Bearer ${process.env.SENDGRID_API_KEY}`,
    'Content-Type': 'application/json',
  }

  const body = {
    from: {
      email: process.env.SENDGRID_FROM_EMAIL,
      name: process.env.SENDGRID_FROM_NAME,
    },
    personalizations: [
      {
        to: [
          {
            email: toEmail,
          },
        ],
        dynamic_template_data: dynamicTemplateData,
      },
    ],
    template_id: templateId,
  }

  const options = {
    method: 'POST',
    headers,
    payload: JSON.stringify(body),
  }

  const response = UrlFetchApp.fetch('https://api.sendgrid.com/v3/mail/send', options)

  Logger.log(response)
}

You can update process.env.SENDGRID_API_KEY, process.env.SENDGRID_FROM_EMAIL, process.env.SENDGRID_FROM_NAME with your SendGrid credentials

Here's what's working for me right now in Google Apps Script, including using a dynamic template and insertion of dynamic data for the "handlebars" in my SendGrid template:

var SENDGRID_KEY ='API_KEY';

var headers = {
  "Authorization" : "Bearer "+SENDGRID_KEY, 
  "Content-Type": "application/json" 
}

function sendEmail_1() {
  
  var body = {
      "personalizations": [
        {
          "to": [
            {
              "email": "test@test.com",
              "name": "Test Name"
            }
          ],
          "bcc": [
            {
              "email": "test@test.com"
            }
          ],
          "dynamic_template_data": 
            {
              "firstName": "Marco Polo"
            }
        }
      ],
      
      "from": 
        {
          "email": "test@test.com",
          "name": "Test Name"
        },
      
      "reply_to": {
          "email": "test@test.com"
        },
      
      "template_id":"TEMPLATE_ID" 
    }
  
  var options = {
    'method':'post',
    'headers':headers,
    'payload':JSON.stringify(body)
  }
  
  var response = UrlFetchApp.fetch("https://api.sendgrid.com/v3/mail/send",options);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!