未收到Azure函数sendgrid电子邮件

时间:2019-05-24 14:31:57

标签: javascript azure azure-functions sendgrid

我有一个Azure功能可以用作网站的联系表单,我的功能似乎可以返回200 OK,但我没有收到电子邮件。

它使用Azure sendgrid绑定,发送网格不显示任何活动,目标邮箱不接收任何内容。

假设我所有的API密钥都是正确的,下面是函数

module.exports = async function (context, req) {
    if (req.body.email) {
        var message = {
            from: {
                email: req.body.email
            },
            subject: "Contact form submission from: " + req.body.name,
            content: [{
                type: 'text/plain',
                value: req.body.message
            }]
        };

        context.done(null, message);

        return {
            res: {
                status: 200
            },
            message: message
        };
    } else {
        return {
            res: {
                status: 400
            }
        };
    }
};

我的function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "message",
      "type": "sendGrid",
      "direction": "out",
      "apiKey" : "ASSUME_THIS_IS_CORRECT",
      "to": "$SomeoneTo@somewhere.tld",
      "from": "$someoneFrom@somewhere.tld",
      "subject": "Someone Mailed Ya!"
  }
  ]
}

我的host.json

{
  "version": "2.0",
  "extensions": {
      "sendGrid": {
          "from": "Full Name <$SomeoneFrom@somwhere.tld>"
      }
  }
}

我给了api密钥完全访问密钥,所以不太确定这是怎么回事?

1 个答案:

答案 0 :(得分:0)

这是我尝试过的相同代码:

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

并且绑定是:

{
  "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
}

在Gmail中,我允许访问安全性较低的应用

enter image description here

成功了。尝试看看是否可行。