'SDK to unsubscribe from marketing emails in AWS Organizations member accounts
I have an AWS Organization and I create member accounts for every new project I make. Since I have control over all of the accounts I use the same email account for all of those, using the [email protected] pattern. This means that I get the same marketing email for every new account I create. I know I can unsubscribe manually, but since I create the member accounts through the CLI I was wondering if there is a way to automatically unsubscribe (or avoid being subscribed) through the SDK.
I've looked in the AWS Organizations SDK documentation, particularly around create-account but haven't found anything relevant.
Solution 1:[1]
apparently there is no solution from AWS for this. The only place I found is this. Which involves manual intervention.
Doing this on Organization ID would be a good option.
meanwhile, I wrote this as a workaround.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
GET_URL = 'https://pages.awscloud.com/communication-preferences'
POST_URL = 'https://pages.awscloud.com/index.php/leadCapture/save2'
s = requests.Session()
def fetch(url, data=None):
if data is None:
return s.get(url).content
return s.post(url, data=data).content
def get_form_id():
forms = BeautifulSoup(fetch(GET_URL), 'html.parser').findAll('form')
for form in forms:
fields = form.findAll('input')
for field in fields:
if field.get('name') == 'formid':
return field['value']
email_id = '[email protected]'
formid = get_form_id()
form_data = {'Email': email_id, 'Unsubscribed': 'yes', 'formid': formid, 'formVid': formid}
r = fetch(POST_URL, data=form_data)
print(r)
Solution 2:[2]
AWS recently changed this web form again breaking things for the existing unsubscribe function I had written. They added 2 new required form data fields: checksum and checksumFields. The checksum field is a sha256 hash of a concatenation of all checksumFields values (single string joined with a ,).
Below is my unsubscribe function written in python.
NOTE: I like @samtoddler's example using beautiful soup to dynamically lookup the formid vs hard-coding it in as a function input var like I have.
def unsubscribe_aws_mkt_emails(email,
url='https://pages.awscloud.com/index.php/leadCapture/save2',
form_id=34006,
lp_id=127906,
sub_id=6,
munchkin_id='112-TZM-766'):
'''
Unsubscribes email from AWS marketing emails via HTTPS POST
'''
sha256_hash = hashlib.sha256()
# Data fields used to calculate the payload SHA256 checksum value
checksum_fields = [
'FirstName',
'LastName',
'Email',
'Company',
'Phone',
'Country',
'preferenceCenterCategory',
'preferenceCenterGettingStarted',
'preferenceCenterOnlineInPersonEvents',
'preferenceCenterMonthlyAWSNewsletter',
'preferenceCenterTrainingandBestPracticeContent',
'preferenceCenterProductandServiceAnnoucements',
'preferenceCenterSurveys',
'PreferenceCenter_AWS_Partner_Events_Co__c',
'preferenceCenterOtherAWSCommunications',
'PreferenceCenter_Language_Preference__c',
'Title',
'Job_Role__c',
'Industry',
'Level_of_AWS_Usage__c',
]
headers = {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'user-agent': 'Mozilla/5.0',
}
data_dict = {
'Email': email,
'preferenceCenterCategory': 'no',
'preferenceCenterGettingStarted': 'no',
'preferenceCenterOnlineInPersonEvents': 'no',
'preferenceCenterMonthlyAWSNewsletter': 'no',
'preferenceCenterTrainingandBestPracticeContent': 'no',
'preferenceCenterProductandServiceAnnoucements': 'no',
'preferenceCenterSurveys': 'no',
'PreferenceCenter_AWS_Partner_Events_Co__c': 'no',
'preferenceCenterOtherAWSCommunications': 'no',
'Unsubscribed': 'yes',
'UnsubscribedReason': 'I already get email from another account',
'unsubscribedReasonOther': 'I already get email from another account',
'zOPEmailValidationHygiene': 'validate',
'formid': form_id,
'formVid': form_id,
'lpId': lp_id,
'subId': sub_id,
'munchkinId': munchkin_id,
'lpurl': '//pages.awscloud.com/communication-preferences.html?cr={creative}&kw={keyword}',
'_mkt_trk': f'id:{munchkin_id}&token:_mch-pages.awscloud.com-1644428507420-99548',
'_mktoReferrer': 'https://pages.awscloud.com/communication-preferences',
'checksumFields': ','.join(checksum_fields),
}
# calculated via js: f.checksum=v("sha256").update(s.join("|")).digest("hex")
# src = https://pages.awscloud.com/js/forms2/js/forms2.min.js
sha256_hash.update('|'.join([data_dict.get(v, '') for v in checksum_fields]).encode())
data_dict['checksum'] = sha256_hash.hexdigest()
data = parse.urlencode(data_dict).encode()
req = request.Request(url, data=data, headers=headers, method='POST')
resp = request.urlopen(req)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ryan Kennedy |
