

支援終止通知：2027 年 3 月 31 日， AWS 將終止對 Amazon WorkMail 的支援。2027 年 3 月 31 日之後，您將無法再存取 Amazon WorkMail 主控台或 Amazon 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 WorkMail Message Flow API 中的 `PutRawMessageContent`動作來更新傳輸中電子郵件訊息的內容。如需 Amazon WorkMail Lambda 函數入門的詳細資訊，請參閱 [設定同步**執行 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，或者您可以將 layer 新增至 Lambda 函數。若要下載正確的 boto3 版本，請參閱 [ GitHub 上的 boto 頁面](https://github.com/boto/boto)。如需新增層的詳細資訊，請參閱[設定函數以使用層](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-using)。  
以下是範例 layer：`"LayerArn":"arn:aws:lambda:${AWS::Region}:489970191081:layer:WorkMailLambdaLayer:2"`。在此範例中，`${AWS::Region}`以適當的 aws 區域取代，例如 us-east-1。

**提示**  
如果您從將 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)從 AWS Serverless Application Repository 部署到您的帳戶開始，系統會在您的帳戶中建立具有必要資源和許可的 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 帳戶 相同的 ，而且它使用與 API 呼叫相同的 AWS 區域。
+ 若要讓 Amazon WorkMail 處理請求，您的 S3 儲存貯體必須具有正確的政策，才能存取 S3 物件。如需詳細資訊，請參閱[Example S3 policy](#s3example)。
+ 使用 [ PutRawMessageContent](https://docs.aws.amazon.com/workmail/latest/APIReference/API_messageflow_PutRawMessageContent.html) API 將更新的訊息內容傳回 Amazon 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)
```

如需分析傳輸中訊息內容之方式的更多範例，請參閱 GitHub 上的 [ amazon-workmail-lambda-templates ](https://github.com/aws-samples/amazon-workmail-lambda-templates) 儲存庫。