3 """Manage email verifications for CU Emails. 5 Perform following operations on a list of emails: 6 - verifyEmails: Sends an email to each of the emails in the 7 list. Amazon verifies that the email is 8 syntaxically correct first. (Each email is 10 - getVerification: Sends Amazon a list of emails and Amazon 11 returns a list of emails and the statuses. 12 (Emails that Amazon doesn't have are not 14 - removeEmails: Sends a request to Amazon to remove email from 15 the list of identities. (Each email is a call 18 Default AWS_REGION is `us-west-2` but the tempalte can be created and verified 19 in other regions by setting up AWS_REGION env variable (check aws 20 docuementation for restrictions in certain regions.) 22 As of 2018-10-02, `HomeCUVerificationEmailTemplate` is the default 23 custom tempalte being and already should exist in the expected aws 24 region before running this script. 26 Fun fact, if you resend email verification request to an email which is already 27 verified, using the custom verification email template, aws ses will change 28 the status from verified to pending and the recipient will have to verify the 29 email again. This is not the case when you send email verifications using 30 aws's default process (i.e. not using custom verification email templates). 38 from botocore.exceptions
import ClientError
41 USAGE_HELP = (
"verifySESEmail.py -e \"email1 email2 email3 ... emailN\" " 42 "-a \"verifyEmails\"|\"getVerification\"|\"removeEmails\"")
46 AWS_REGION = os.getenv(
'AWS_REGION',
'us-west-2')
49 CUSTOM_EMAIL_TEMPLATE =
"HomeCUVerificationEmailTemplate" 53 """Custom ArgumentParser class 56 argparse.ArgumentParser 60 """Suppressing default error method 62 Return custom error message on ArgumentParser.error. 65 message -- error message 75 print(json.dumps(custom_error))
80 """Prepare argument parser. 83 parser -- ArgumentParser object 86 description=
"AWS SES email verification using custom template." 89 parser.add_argument(
"-e",
"--emails",
90 help=
"Space delimited list of emails",
94 parser.add_argument(
"-a",
"--action",
95 choices=[
"verifyEmails",
96 "getVerification",
"removeEmails"],
98 help=(
'Action of the script. ' 99 '(i) "verifyEmails" -- Sends an email to ' 100 'each of the `emails` in the list. Amazon ' 101 'verifies that the email is syntaxically correct' 102 ' first. (Each email is a call to AWS SES. ' 103 '(ii) "getVerification" -- Sends Amazon a list' 104 ' of emails and Amazon returns a list of emails' 105 ' and the statuses (Emails that Amazon doesn\'t' 106 ' have are not returned). ' 107 '(iii) "removeEmails" -- Sends a request to ' 108 'Amazon to remove email from the list of ' 109 'identities (Each email is a call to AWS SES.))' 113 parser.add_argument(
"-tn",
"--template-name",
114 help=
"Provide a template name to use",
115 default=CUSTOM_EMAIL_TEMPLATE)
121 """Main method to execute operations on email verification. 124 argv {list} -- list of script arguments 140 args = parser.parse_args(argv)
141 except (argparse.ArgumentError, Exception)
as e:
147 print(json.dumps(main_return))
156 client = boto3.client(
'ses', region_name=AWS_REGION)
165 for email
in args.emails.split():
167 email = email.strip()
169 if (args.action ==
"verifyEmails"):
173 if (email
not in response):
174 response[email] = client.send_custom_verification_email(
176 TemplateName=args.template_name
178 except ClientError
as e:
179 response[email] = e.response[
"Error"][
"Message"]
180 raise BaseException(email +
" is not valid")
182 elif (args.action ==
"removeEmails"):
185 if (email
not in response):
186 response[email] = client.delete_identity(
188 except ClientError
as e:
189 response[email] = e.response[
"Error"][
"Message"]
190 raise BaseException(email +
" is not valid")
192 elif (args.action ==
"getVerification"):
194 if (email
not in response):
195 response[email] =
True 197 if (args.action ==
"getVerification"):
199 response = list(response.keys())
201 response = client.get_identity_verification_attributes(
204 except ClientError
as e:
205 raise BaseException(e)
207 except BaseException
as e:
208 main_return[
'status'] =
'999' 209 main_return[
'error'] = str(e)
211 except Exception
as e:
212 main_return[
'status'] =
'999' 213 main_return[
'error'] = e
217 main_return[
'response'] = response
218 print (json.dumps(main_return))
225 """Script entrypoint""" 229 if __name__ ==
"__main__":