Send emails using Sendgrid with google appscript

前端 未结 3 833
别那么骄傲
别那么骄傲 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:40

    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

    0 讨论(0)
  • 2021-02-06 18:41

    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

    0 讨论(0)
  • 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);
    }

    0 讨论(0)
提交回复
热议问题