Send emails using Sendgrid with google appscript

淺唱寂寞╮ 提交于 2019-12-03 14:04:39

问题


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 Appscript. This is the code I am using, but it is no good.

var data = {

        "api_user":"username",
        "api_key":"ioioi",
        "to":[],
        "tonnage":[],
        "cc":[],
        "ccname":[],
        "bcc":[],
        "subject":sub,
        "from":from,
        "html":htmlBody
      }

      var headers = { "Accept":"application/json", 
                     "Content-Type":"application/json"
                    };



        data = JSON.stringify(data);

        var options = {
          "method": "POST",
          "payload": data,
          "headers": headers,
          "muteHttpExceptions": true
        };

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

Does anyone have any idea or code to send emails with sendgrid using googl appscript?


回答1:


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




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/37249209/send-emails-using-sendgrid-with-google-appscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!