1

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

kim
  • 3,385
  • 2
  • 15
  • 21
  • Could you provide your `SendGrid` binding? – kamil-mrzyglod Apr 19 '18 at 07:06
  • 1
    Are there any errors returned by the API? I created a short PowerShell script for doing the same https://gist.github.com/kimpihlstrom/145304ebb00311270b3c6f7a99d4c606. Check it out, in case it's of any help. – kim Apr 19 '18 at 07:48

1 Answers1

4

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

enter image description here

Click this link, you could configure it.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30