以下是使用AWS SES和AWS Lambda动态创建转发到外部地址的邮箱的解决方案:
import boto3
def lambda_handler(event, context):
# 获取要创建的邮箱地址和要转发的外部地址
mailbox = event['mailbox']
forwarding_address = event['forwarding_address']
# 创建一个新的邮箱规则
client = boto3.client('ses')
response = client.create_receipt_rule(
RuleSetName='default-rule-set',
Rule={
'Name': mailbox,
'Enabled': True,
'Recipients': [
mailbox,
],
'Actions': [
{
'S3Action': {
'TopicArn': 'arn:aws:sns:us-east-1:123456789012:MyTopic',
'BucketName': 'email-bucket',
'ObjectKeyPrefix': 'email/',
'EncryptionConfiguration': {
'S3EncryptionAction': {
'KmsKeyArn': 'arn:aws:kms:us-east-1:123456789012:key/12345678-1234-5678-1234-567812345678'
}
}
}
},
{
'LambdaAction': {
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:MyLambdaFunction'
}
},
{
'AddHeaderAction': {
'HeaderName': 'X-SES-MESSAGE-TAGS',
'HeaderValue': 'mailbox={}'.format(mailbox)
}
},
{
'BounceAction': {
'SmtpReplyCode': '550',
'Message': 'Mailbox does not exist'
}
},
{
'WorkmailAction': {
'OrganizationArn': 'arn:aws:workmail:us-east-1:123456789012:organization/OrganizationId',
'TopicArn': 'arn:aws:sns:us-east-1:123456789012:MyTopic'
}
},
{
'StopAction': {}
}
]
}
)
# 添加转发规则到新创建的邮箱
response = client.create_receipt_rule_set(
RuleSetName='default-rule-set',
Rules=[
{
'Name': 'forwarding-rule',
'Enabled': True,
'Recipients': [
mailbox,
],
'Actions': [
{
'S3Action': {
'TopicArn': 'arn:aws:sns:us-east-1:123456789012:MyTopic',
'BucketName': 'email-bucket',
'ObjectKeyPrefix': 'email/',
'EncryptionConfiguration': {
'S3EncryptionAction': {
'KmsKeyArn': 'arn:aws:kms:us-east-1:123456789012:key/12345678-1234-5678-1234-567812345678'
}
}
}
},
{
'LambdaAction': {
'FunctionArn': 'arn:aws:lambda:us-east-1:123456789012:function:MyLambdaFunction'
}
},
{
'AddHeaderAction': {
'HeaderName': 'X-SES-MESSAGE-TAGS',
'HeaderValue': 'mailbox={}'.format(mailbox)
}
},
{
'BounceAction': {
'SmtpReplyCode': '550',
'Message': 'Mailbox does not exist'
}
},
{
'WorkmailAction': {
'OrganizationArn': 'arn:aws:workmail:us-east-1:123456789012:organization/OrganizationId',
'TopicArn': 'arn:aws:sns:us-east-1:123456789012:MyTopic'
}
},
{
'StopAction': {}
}
]
}
]
)
# 创建一个转发规则,将收到的邮件转发到外部地址
response = client.create_receipt_rule(
RuleSetName='default-rule-set',
Rule={
'Name': 'forwarding-rule',
'Enabled': True,
'Recipients': [
mailbox,
],
'Actions': [
{
'WorkmailAction': {
'OrganizationArn': 'arn:aws:workmail:us-east-1:123456789012:organization/O