【问题标题】:How can I prevent cognito from sending automatic emails?如何防止 cognito 发送自动电子邮件?
【发布时间】:2021-07-04 17:00:19
【问题描述】:

我在Custom message 触发器上有一个 lambda,这是处理程序:

def forgot_password(event, context):

    boto_client = boto3.client('ses')
    html_body = get_html_file_from_bucket()
    email = event['request']['userAttributes']['email']
    code_parameter = event['request']['codeParameter']

    url_data = {
        "username": email,
        "code_parameter": code_parameter
    }

    encoded_params = urllib.parse.urlencode(url_data)
    set_new_password_link = f'{os.environ["CONCIL_SET_NEW_PASSWORD_ENDPOINT"]}?{encoded_params}'
    html_body = html_body.replace(
        "{replace_me}", set_new_password_link)

    if event["userPoolId"] == os.environ['COGNITO_USER_POOL_ID']:
        if event["triggerSource"] == "CustomMessage_ForgotPassword":
            set_new_password_email_response = boto_client.send_email(
                Destination={
                    'ToAddresses': [
                        email
                    ],
                },
                Message={
                    'Body': {
                        'Html': {
                            'Charset': "utf-8",
                            'Data': html_body,
                        },
                    },
                    'Subject': {
                        'Charset': "utf-8",
                        'Data': "Redefina a sua senha",
                    },
                },
                Source=os.environ["CONCIL_NOTIFICATION_EMAIL"]
            )
            modify_response(
                event, "emailSubject", None)
            modify_response(
                event, "emailMessage", None)
            print("SES RESPONSE")
            print(set_new_password_email_response)
        else:
            return event
    print("EVENT")
    print(event)
return event

如您所见,我通过手动发送 AWS SES 来覆盖默认发送行为,我这样做是因为 cognito/SES 关系的可恶错误默认行为(即使我将 emailMessage 设置为 HTML 正文,该消息将被默认消息替换)。

问题是,现在我收到 2 封电子邮件:正确的一封,以及自动不必要的一封。那么,我怎样才能抑制自动呢?在用户池配置中,这是当前默认值:

"SmsVerificationMessage": "Your verification code is {####}. ",
    "EmailVerificationMessage": "Seu código de verificação é {####}.",
    "EmailVerificationSubject": "Seu código de verificação",
    "VerificationMessageTemplate": {
        "SmsMessage": "Your verification code is {####}. ",
        "EmailMessage": "Seu código de verificação é {####}.",
        "EmailSubject": "Seu código de verificação",
        "DefaultEmailOption": "CONFIRM_WITH_CODE"
    },

我在文档的任何地方都找不到完全删除这个烦人的自动电子邮件的方法。

【问题讨论】:

  • 也许尝试引发异常?
  • @jellycsc,这主意太牛了,我喜欢HUAHUAHUA我会试试的。
  • xxxxxxDDDDDDDDD
  • @jellycsc 工作就像一个魅力,先生,你是个天才。我们在我国(巴西)为这种解决方案取了一个名字,叫做“gambiarra”。 AWS 上的开发者应该更加体谅那些使用他们服务的可怜的开发者。
  • TIL。是的,我之前对 Cognito 的体验也很糟糕。哈哈

标签: python amazon-web-services boto3 amazon-cognito


【解决方案1】:

根据OP的评论,解决方案是在lambda函数返回之前引发异常。

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 2011-10-22
    • 2013-08-22
    相关资源
    最近更新 更多