

终止支持通知：2027 年 3 月 31 日， AWS 将终止对亚马逊 WorkMail的支持。2027 年 3 月 31 日之后，您将无法再访问亚马逊 WorkMail 控制台或亚马逊 WorkMail 资源。有关更多信息，请参阅 [Amazon WorkMail 终止支持](https://docs.aws.amazon.com/workmail/latest/adminguide/workmail-end-of-support.html)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 AWS Lambda 更新邮件内容
<a name="update-with-lambda"></a>

配置同步 AWS Lambda 函数来管理电子邮件流后，您可以使用 Amazon Mess WorkMail age Flow API 中的`PutRawMessageContent`操作来更新传输中的电子邮件的内容。有关开始使用适用于 Amazon 的 Lambda 函数的更多信息 WorkMail，请参阅。[配置同步**运行 Lambda** 规则](lambda.md#synchronous-rules)有关 API 的更多信息，请参阅 [ PutRawMessageContent](https://docs.aws.amazon.com/workmail/latest/APIReference/API_messageflow_PutRawMessageContent.html)。

**注意**  
 PutRawMessageContent API 需要 boto3 1.17.8，或者你可以在 Lambda 函数中添加一个层。要下载正确的 boto3 版本，请参阅上的 [boto 页面。 GitHub](https://github.com/boto/boto)有关添加层的更多信息，请参阅[配置函数以使用层](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-using)。  
下面是一个示例层：`"LayerArn":"arn:aws:lambda:${AWS::Region}:489970191081:layer:WorkMailLambdaLayer:2"`。在此示例中，将 `${AWS::Region}` 替换为适当的 AWS 区域，如 us-east-1。

**提示**  
如果您首先将 AWS Serverless 应用程序存储库中的 Amazon WorkMail [Hello World Lambda 函数](https://console.aws.amazon.com/lambda/home#/create/app?applicationId=arn:aws:serverlessrepo:us-east-1:489970191081:applications/workmail-hello-world-python)部署到您的账户，则系统会在您的账户中创建一个具有必要资源和权限的 Lambda 函数。然后，您可以根据使用案例将业务逻辑添加到 lambda 函数。

操作时请记住以下几点：
+ 使用 [ GetRawMessageContent](https://docs.aws.amazon.com/workmail/latest/APIReference/API_messageflow_GetRawMessageContent.html)API 检索原始消息内容。有关更多信息，请参阅 [使用检索消息内容 AWS Lambda](lambda-content.md)。
+ 获得原始邮件后，请更改 MIME 内容。完成后，将邮件上传到您账户中的 Amazon Simple Storage Service (Amazon S3) 存储桶。确保 S3 存储桶使用与您的 Amazon WorkMail 操作 AWS 账户 相同的 AWS 区域，并确保它使用与您的 API 调用相同的 AWS 区域。
+  WorkMail 为了让 Amazon 处理请求，您的 S3 存储桶必须具有正确的策略才能访问 S3 对象。有关更多信息，请参阅 [Example S3 policy](#s3example)。
+ 使用 [ PutRawMessageContent](https://docs.aws.amazon.com/workmail/latest/APIReference/API_messageflow_PutRawMessageContent.html)API 将更新后的消息内容发送回亚马逊 WorkMail。

**注意**  
`PutRawMessageContent`API 可确保更新消息的 MIME 内容符合 RFC 标准以及[RawMessageContent](https://docs.aws.amazon.com/workmail/latest/APIReference/API_messageflow_RawMessageContent.html)数据类型中提及的标准。入站到您的 Amazon WorkMail 组织的电子邮件并不总是符合这些标准，因此 `PutRawMessageContent` API 可能会拒绝这些标准。在这种情况下，您可以查阅返回的错误消息，了解有关如何解决任何问题的更多信息。

**Example S3 策略示例**    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "workmail.REGION.amazonaws.com"
            },
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": "arn:aws:s3:::My-Test-S3-Bucket/*",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "111122223333"
                },
                "Bool": {
                    "aws:SecureTransport": "true"
                },
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:workmailmessageflow:us-east-1:111122223333:message/WORKMAIL_ORGANIZATION_ID/*"
                }
            }
        }
    ]
}
```

以下示例显示了 Lambda 函数如何使用 Python 运行时更新传输中电子邮件的主题。

```
    import boto3
    import os
    import uuid
    import email
     
    def email_handler(event, context):
        workmail = boto3.client('workmailmessageflow', region_name=os.environ["AWS_REGION"])
        s3 = boto3.client('s3', region_name=os.environ["AWS_REGION"])
        
        msg_id = event['messageId']
        raw_msg = workmail.get_raw_message_content(messageId=msg_id)
        parsed_msg = email.message_from_bytes(raw_msg['messageContent'].read())
        
        # Updating subject. For more examples, see https://github.com/aws-samples/amazon-workmail-lambda-templates.
        parsed_msg.replace_header('Subject', "New Subject Updated From Lambda")
       
        # Store updated email in S3
        key = str(uuid.uuid4());
        s3.put_object(Body=parsed_msg.as_bytes(), Bucket="amzn-s3-demo-bucket", Key=key)
     
        # Update the email in WorkMail
        s3_reference = {
            'bucket': "amzn-s3-demo-bucket",
            'key': key
        }
        content = {
            's3Reference': s3_reference
        }
        workmail.put_raw_message_content(messageId=msg_id, content=content)
```

有关分析传输中消息内容的更多方法示例，请参阅中的[ amazon-workmail-lambda-templates](https://github.com/aws-samples/amazon-workmail-lambda-templates)存储库。 GitHub