1

在这篇文章的指导下,我编写了以下 queuetrigger 代码,用于在消息排队时发送电子邮件。

import logging
import sendgrid
import azure.functions as func
import os
def main(msg: func.QueueMessage) -> None:
    logging.info('Python queue trigger function processed a queue item: %s',
                 msg.get_body().decode('utf-8'))
    
   
    data = {
        "personalizations": [
          {
            "to": [
              {
                "email": "rrrrrrrr"
              }
            ],
            "subject": "Sending with SendGrid is Fun"
          }
        ],
        "from": {
          "email": "ghyu"
        },
        "content": [
          {
            "type": "text/plain",
            "value": "and easy to do anywhere, even with Python"
          }
         ]
    }
    print('Sending email using SendGrid:', data)
    with open(os.environ[_AZURE_FUNCTION_SENDGRID_OUTPUT_ENV_NAME], 'wb') as f:
      json.dump(data,f)

函数.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "outqueue1",
      "connection": "storageaccountautom92bb_STORAGE"
    },
    
    {
      
      "name": "outputMessage",
      "type": "sendGrid",
      "from": "ghyu",
      "apiKey": "MY_SENDGRID_API_KEY",
      "direction": "out"
    }
  ],
  "disabled": false
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "hjk",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "storageaccountautom92bb_STORAGE": "hjk",
    "MY_SENDGRID_API_KEY": "tyhuE",
    "_AZURE_FUNCTION_SENDGRID_OUTPUT_ENV_NAME" : "GIS klop",
    "_AZURE_FUNCTION_QUEUE_INPUT_ENV_NAME" : "msg"
  }
}

尽管该功能在消息排队时响应消息,但它无法发送电子邮件。它抛出一个错误;

the following parameters are declared in function.json but not in Python: {'outputMessage'}
  1. 我怎样才能最好地解决这个问题?
  2. 外部绑定是否正确?
4

1 回答 1

1

Azure Function 禁止了发送邮件的端口,所以我们必须使用 sendgrid 发送邮件。(这是第三方工具,但已集成到 azure 函数绑定中,所以我们可以直接使用它。)

例如,如果您想从电子邮件 A 向电子邮件 B 发送电子邮件。

首先,进入sendgrid 网站,创建发件人并验证电子邮件 A:

在此处输入图像描述

之后,电子邮件 A 就可以通过 sendgrid 发送电子邮件了。

现在我们需要生成一个 SendGrid API 密钥并复制和存储 API 密钥:(这将填充到 local.settings.json 的 Values 部分作为环境变量读取。)

在此处输入图像描述

然后,您可以使用它发送电子邮件:

host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 3.1.0)"
  }
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "SendGrid_API_Key": "SG._-yYnhzER2SEbAvzOxSHnA.xxxxxx",
    "0730bowmanwindow_STORAGE": "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net"
  }
}

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue",
      "connection": "0730bowmanwindow_STORAGE"
    },
    {
      "type": "sendGrid",
      "name": "sendGridMessage",
      "direction": "out",
      "apiKey": "SendGrid_API_Key",
      "from": "emailA@emailA.com"
    }
  ]
}

__init__.py

import logging
import json
import azure.functions as func


def main(msg: func.QueueMessage, sendGridMessage: func.Out[str]) -> None:
    logging.info('Python queue trigger function processed a queue item: %s',
                 msg.get_body().decode('utf-8'))
    value = "Sent from Azure Functions"

    message = {
        "personalizations": [ {
          "to": [{
            "email": "emailB@emailB.com"
            }]}],
        "subject": "Azure Functions email with SendGrid",
        "content": [{
            "type": "text/plain",
            "value": value }]
    }
    sendGridMessage.set(json.dumps(message))

之后,我向“myqueue”发送消息,emailB 收到电子邮件:

在此处输入图像描述

顺便说一句,这只能保证成功投递,因为有些邮箱拒绝接受通过sendgrid发送的邮件。这种情况下,你还是会收到200响应,但是收件人不会收到邮件。(这种情况下收件人需要去邮箱设置解除相关限制。)

于 2020-11-19T06:48:56.873 回答