Javascript Azure Function 使用 SendGrid 发送电子邮件

use*_*254 2 javascript azure sendgrid azure-functions

我想使用 SendGrid 从 Azure 函数 (Javascript) 发送电子邮件。我做了以下事情

  1. 为 SendGrid API 密钥创建了一个新的 AppSettings
  2. Azure Function 的 SendGrid 输出绑定集
  3. 以下是我的Azure功能

 

    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);
};
Run Code Online (Sandbox Code Playgroud)

但电子邮件没有发送。请提供一些指示

Joe*_*Cai 5

我最初测试并遇到与你同样的问题。

请改为context.done(null, {message});

您可以尝试使用以下代码:

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});
};
Run Code Online (Sandbox Code Playgroud)

function.json 文件是:

{
  "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
}
Run Code Online (Sandbox Code Playgroud)

这里我使用Gmail,所以我也允许不太安全的应用程序:ON

在此输入图像描述

点击此链接,您可以进行配置。