

지원 종료 공지: 2027 AWS 년 3월 31일에는 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 메시지 흐름 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이 필요합니다. 또는 Lambda 함수에 계층을 추가할 수 있습니다. 올바른 boto3 버전을 다운로드하려면 [GitHub의 boto 페이지](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}`을 us-east-1과 같은 적절한 AWS 리전으로 대체하세요.

**작은 정보**  
AWS Serverless Application Repository에서 사용자 계정으로 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(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) 리포지토리를 참조하세요.