aws / aws-cli

Universal Command Line Interface for Amazon Web Services
Other
15.09k stars 4.01k forks source link

`aws sesv2 list-contacts` returns an empty result when filtering with `FilteredStatus=OPT_OUT` #8742

Open delhi09 opened 2 weeks ago

delhi09 commented 2 weeks ago

Describe the bug

I would like to count OPT_OUT emails in the list management of AWS SES.

I confirmed at least one OPT_OUT email exists using the following command:

aws sesv2 get-contact --contact-list-name=TestContactList --email-address=test@example.com --region us-east-1
{
    "ContactListName": "TestContactList",
    "EmailAddress": "test@example.com",
    "TopicPreferences": [
        {
            "TopicName": "test-topic",
            "SubscriptionStatus": "OPT_OUT"
        }
    ],
    "UnsubscribeAll": false,
    "CreatedTimestamp": "2024-06-05T11:17:46.771000+09:00",
    "LastUpdatedTimestamp": "2024-06-13T12:52:48.739000+09:00"
}

However, the following command always returns an empty result:

aws sesv2 list-contacts --contact-list-name=TestContactList --filter="FilteredStatus=OPT_OUT,TopicFilter={TopicName=test-topic,UseDefaultIfPreferenceUnavailable=true}" --no-paginate --region us-east-1
{
    "Contacts": [],
    "NextToken": "***"
}

When FilteredStatus=OPT_IN, the command successfully returns OPT_IN emails as follows:

aws sesv2 list-contacts --contact-list-name=TestContactList --filter="FilteredStatus=OPT_IN,TopicFilter={TopicName=test-topic,UseDefaultIfPreferenceUnavailable=true}" --no-paginate --region us-east-1
{
    "Contacts": [
        {
            "EmailAddress": "test1@example.com",
            "TopicPreferences": [
                {
                    "TopicName": "test-topic",
                    "SubscriptionStatus": "OPT_IN"
                }
            ],
            "UnsubscribeAll": false,
            "LastUpdatedTimestamp": "2024-05-23T19:02:54.560000+09:00"
        },
        {
            "EmailAddress": "test2@example.com",
            "TopicPreferences": [
                {
                    "TopicName": "test-topic",
                    "SubscriptionStatus": "OPT_IN"
                }
            ],
            "UnsubscribeAll": false,
            "LastUpdatedTimestamp": "2024-05-23T20:01:50.994000+09:00"
        },
...

Expected Behavior

OPT_OUT emails should be returned as follows:

aws sesv2 list-contacts --contact-list-name=TestContactList --filter="FilteredStatus=OPT_OUT,TopicFilter={TopicName=test-topic,UseDefaultIfPreferenceUnavailable=true}" --no-paginate --region us-east-1
{
    "Contacts": [
        {
            "EmailAddress": "test@example.com",
            "TopicPreferences": [
                {
                    "TopicName": "test-topic",
                    "SubscriptionStatus": "OPT_OUT"
                }
            ],
            "UnsubscribeAll": false,
            "LastUpdatedTimestamp": "2024-05-23T19:02:54.560000+09:00"
        },
...

Current Behavior

As described in the "Describe the bug" section.

Reproduction Steps

1.Send an email using the SESv2 API with ListManagementOptions:

import boto3

client = boto3.client("sesv2", region_name="us-east-1")

args = {
        "FromEmailAddress": "from@example.com",
        "Destination": {
            "ToAddresses": ["test@example.com"],
        },
        "Content": {
            "Simple": {
                "Subject": {"Data": "test", "Charset": "UTF-8"},
                "Body": {
                    "Text": {"Data": "test", "Charset": charset},
                    "Html": {"Data": "<div>test</test>", "Charset": "UTF-8"},
                },
            },
        },
        "ListManagementOptions": {
            "ContactListName": "TestContactList",
            "TopicName": "test@example.com",
        }
}
client.send_email(**args)

2.Receive the email in a Gmail client and click the "Unsubscribe" link.

3.Confirm "test@example.com" is saved as OPT_OUT using the following command:

aws sesv2 get-contact --contact-list-name=TestContactList --email-address=test@example.com --region us-east-1
{
    "ContactListName": "TestContactList",
    "EmailAddress": "test@example.com",
    "TopicPreferences": [
        {
            "TopicName": "test-topic",
            "SubscriptionStatus": "OPT_OUT"
        }
    ],
    "UnsubscribeAll": false,
    "CreatedTimestamp": "***",
    "LastUpdatedTimestamp": "***"
}
  1. Fetch OPT_OUT emails using the following command. It will probably return an empty result:
    aws sesv2 list-contacts --contact-list-name=TestContactList --filter="FilteredStatus=OPT_OUT,TopicFilter={TopicName=test-topic,UseDefaultIfPreferenceUnavailable=true}" --no-paginate --region us-east-1
    {
    "Contacts": [],
    "NextToken": "***"
    }

Possible Solution

No response

Additional Information/Context

No response

CLI version used

aws-cli/2.16.7 Python/3.11.8 Darwin/23.4.0 exe/x86_64

Environment details (OS name and version, etc.)

macOS 14.4.1

RyanFitzSimmonsAK commented 1 week ago

Hi @delhi09, thanks for reaching out. Since you received a NextToken, I suspect what's happening here is that pagination is taking place before filtering. You're using the --no-paginate flag, so if your OPT_OUT contact is not in the first page of results, getting no results back makes sense. If you remove that, or continue paginating, what's the response look like?

delhi09 commented 1 week ago

Hi @RyanFitzSimmonsAK , thank you for your suggestion. I tried it, but unfortunately, it didn't change the result.

$ aws sesv2 list-contacts --contact-list-name=TestContactList --filter="FilteredStatus=OPT_OUT,TopicFilter={TopicName=test-topic,UseDefaultIfPreferenceUnavailable=true}" --region us-east-1
{
    "Contacts": [],
    "NextToken": “xxx”
}
RyanFitzSimmonsAK commented 1 week ago

What if you continue to paginate using the NextToken?