您可以在AWS Cognito中使用Lambda函数自定义电子邮件发送者,但是在更新Lambda配置时可能会遇到错误。以下是一些解决方法:
确保您在AWS Lambda中创建了CustomEmailSender函数,并为该函数分配了正确的IAM角色。
打开AWS管理控制台,进入AWS Cognito用户池的设置页面,然后转到"Triggers"选项卡。选择"Custom Email Sender",然后在Lambda Function名称字段中输入CustomEmailSender函数的名称。
在Lambda Function ARN字段中输入CustomEmailSender函数的ARN。ARN是一个唯一的标识符,用于标识函数。
确保您的Lambda函数代码中包含您的用户池ID和区域设置,以便正确地构造电子邮件。
以下是一个示例Lambda函数代码,其中包含了以上解决方法中的所有必要信息:
import boto3
from botocore.exceptions import ClientError
import os
SENDER = "MyCompany "
CONFIGSET = "myConfigSet"
region = os.environ['AWS_REGION']
user_pool_id = os.environ['USER_POOL_ID']
CHARSET = "UTF-8"
def lambda_handler(event, context):
client = boto3.client('cognito-idp')
# Get user record
user = event['userAttributes']
# Construct email message
body = "Hello {},\n\n" \
"This is a test email.\n\n" \
"Regards,\n" \
"MyCompany".format(user['name'])
subject = "Test Email"
destination = {
'ToAddresses': [
user['email']
]
}
try:
# Send email using AWS SES
client.send_email(
Source=SENDER,
Destination=destination,
Message={
'Body': {
'Text': {
'Data': body,
'Charset': CHARSET
}
},
'Subject': {
'Data': subject,
'Charset': CHARSET
}
},
ConfigurationSetName=CONFIGSET
)
except ClientError as e:
print(e.response['Error']['Message'])
# Return result to Cognito
return event