Send emails using Sendgrid with google appscript

前端 未结 3 832
别那么骄傲
别那么骄傲 2021-02-06 18:26

I am creating a googlesheet addon to send mails.And for sending mails I am using sendgrid. I cannot find any documentation or example code for sending mails with Google Appscrip

3条回答
  •  别那么骄傲
    2021-02-06 18:43

    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);
    }

提交回复
热议问题