Javascript Azure Function to send email using SendGrid

若如初见. 提交于 2020-01-24 00:51:05

问题


I want to send emails from an Azure function (Javascript) using SendGrid. I have done the following

  1. created a new AppSettings for SendGrid API Key
  2. SendGrid output binding set of Azure Function
  3. Following is my Azure Function

 

    module.exports = function (context, myQueueItem) {
var message = {
         "personalizations": [ { "to": [ { "email": "testto@test.com" } ] } ],
        from: { email: "testfrom@test.com" },        
        subject: "Azure news",
        content: [{
            type: 'text/plain',
            value: myQueueItem
        }]
    };
    context.done(null, message);
};

But email is not getting send. Please provide some pointers


回答1:


I test and face the same problem with you initially.

Please change to context.done(null, {message});

You could try to use the following code:

module.exports = function (context, order) {    
    context.log(order);
    var message = {
         "personalizations": [ { "to": [ { "email": "testto@gmail.com" } ] } ],
        from: { email: "testfrom@gmail.com" },        
        subject: "Azure news",
        content: [{
            type: 'text/plain',
            value: order
        }]
    };

    context.done(null, {message});
};

And the funtion.json file is:

{
  "bindings": [
    {
      "type": "queueTrigger",
      "name": "order",
      "direction": "in",
      "queueName": "samples-orders"
    },
    {
      "type": "sendGrid",
      "name": "message",
      "direction": "out",
      "apiKey": "mysendgridkey",
      "from": "testfrom@gmail.com",
      "to": "testto@gmail.com"
    }
  ],
  "disabled": false
}

Here I use the Gmail, so I also Allow less secure apps: ON

Click this link, you could configure it.



来源:https://stackoverflow.com/questions/49914465/javascript-azure-function-to-send-email-using-sendgrid

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