3 """ Create AWS SES custom verification email template 5 This script is used to mimic the following aws ses custom verification 6 email template creation function: 8 aws ses create-custom-verification-email-template 9 --template-name <template-name> 10 --from-email-address <from-email-address> 11 --template-subject <template-subject> 12 --template-content <template-content> 13 --success-redirection-url <success-redirection-url> 14 --failure-redirection-url <failure-redirection-url> 17 Default AWS_REGION is `us-west-2` but the tempalte can be created in other 18 regions by setting up AWS_REGION env variable (check aws docuementation 19 for restrictions in certain regions.) 21 As os 2018-10-02, Update and Deletion of existing templates need to be 22 performed using the aws account with sufficient permissions to perform those 23 operations. If the local dev user is decided to grant such permissions in 24 the future, this script can be extended for deletion and update of the 25 existing custom verification email templates. 27 As of 2018-10-02, you can only run 4 28 `create-custom-verification-email-template` aws ses requests in a day in each 37 from botocore.exceptions
import ClientError
41 AWS_REGION = os.getenv(
'AWS_REGION',
'us-west-2')
45 VERIFICATION_TEMPLATE = os.getenv(
'DEFAULT_VERIFICATION_TEMPLATE_PATH',
46 (
'/home/homecu/tmp/aws/ses/' 47 'default_verification_email_template'))
49 USAGE = (
"USAGE: CreateCustomVerificationTemplate.py [-h] -tn TEMPLATE_NAME " 50 "[-fe FROM_EMAIL_ADDRESS] [-su SUBJECT] [-c CONTENT]" 51 "[-s SUCCESS] [-f FAILURE]")
55 '''return custom email verification template defaults''' 57 with open(VERIFICATION_TEMPLATE,
'r')
as fh:
58 contents =
''.join(fh.readlines())
61 "sender":
"support@homecu.com",
62 "subject":
"Please confirm your email address",
64 "success": (
"https://my.homecu.net/admbin/static/html/" 65 "AdmEmailSuccess.html"),
66 "failure": (
"https://my.homecu.net/admbin/static/html/" 67 "AdmEmailFailure.html"),
72 """Custom ArgumentParser class 75 argparse.ArgumentParser 79 """Suppressing default error method 81 Return custom error message on ArgumentParser.error. 84 message -- error message 94 print(json.dumps(
"{}: {}".format(__file__, custom_error)))
99 """Prepare argument parser. 102 parser -- ArgumentParser object 106 description=
"AWS SES Create New Custom Email Verification Template." 109 parser.add_argument(
"-tn",
"--template-name",
110 help=(
'Name of the custom verification email template' 115 parser.add_argument(
"-fe",
"--from-email-address",
116 help=
"Sender's email address (from email address)",
117 default=defaults[
"sender"]
120 parser.add_argument(
"-su",
"--subject",
121 help=
"Email subject",
122 default=defaults[
"subject"]
125 parser.add_argument(
"-c",
"--content",
126 help=(
'Email content (single lined json text with no ' 128 default=defaults[
"content"]
131 parser.add_argument(
"-s",
"--success",
132 help=
"Success redirection url",
133 default=defaults[
"success"]
136 parser.add_argument(
"-f",
"--failure",
137 help=
"Failure redirection url",
138 default=defaults[
"failure"]
144 """Main method to create custom verification template 147 argv {list} -- list of script arguments 150 SystemExit -- [description] 161 args = parser.parse_args(argv)
162 except (argparse.ArgumentError, Exception)
as e:
168 print(json.dumps(main_return))
176 client = boto3.client(
'ses', region_name=AWS_REGION)
179 response = client.create_custom_verification_email_template(
180 TemplateName=args.template_name,
181 FromEmailAddress=args.from_email_address,
182 TemplateSubject=args.subject,
183 TemplateContent=args.content,
184 SuccessRedirectionURL=args.success,
185 FailureRedirectionURL=args.failure
188 except ClientError
as e:
189 main_return[
'status'] =
'999' 190 main_return[
'error'] = e.response[
"Error"][
"Message"]
192 except BaseException
as e:
193 main_return[
'status'] =
'999' 194 main_return[
'error'] = str(e)
196 except Exception
as e:
197 main_return[
'status'] =
'999' 198 main_return[
'error'] = e
201 main_return[
"response"] = response
202 print(json.dumps(main_return))
209 """Script entrypoint""" 213 if __name__ ==
"__main__":