Encapsulates Amazon SES template functions.
| 22 | |
| 23 | # snippet-start:[python.example_code.ses.SesTemplate] |
| 24 | class SesTemplate: |
| 25 | """Encapsulates Amazon SES template functions.""" |
| 26 | |
| 27 | def __init__(self, ses_client): |
| 28 | """ |
| 29 | :param ses_client: A Boto3 Amazon SES client. |
| 30 | """ |
| 31 | self.ses_client = ses_client |
| 32 | self.template = None |
| 33 | self.template_tags = set() |
| 34 | |
| 35 | def _extract_tags(self, subject, text, html): |
| 36 | """ |
| 37 | Extracts tags from a template as a set of unique values. |
| 38 | |
| 39 | :param subject: The subject of the email. |
| 40 | :param text: The text version of the email. |
| 41 | :param html: The html version of the email. |
| 42 | """ |
| 43 | self.template_tags = set(re.findall(TEMPLATE_REGEX, subject + text + html)) |
| 44 | logger.info("Extracted template tags: %s", self.template_tags) |
| 45 | |
| 46 | # snippet-end:[python.example_code.ses.SesTemplate] |
| 47 | |
| 48 | def verify_tags(self, template_data): |
| 49 | """ |
| 50 | Verifies that the tags in the template data are part of the template. |
| 51 | |
| 52 | :param template_data: Template data formed of key-value pairs of tags and |
| 53 | replacement text. |
| 54 | :return: True when all of the tags in the template data are usable with the |
| 55 | template; otherwise, False. |
| 56 | """ |
| 57 | diff = set(template_data) - self.template_tags |
| 58 | if diff: |
| 59 | logger.warning( |
| 60 | "Template data contains tags that aren't in the template: %s", diff |
| 61 | ) |
| 62 | return False |
| 63 | else: |
| 64 | return True |
| 65 | |
| 66 | def name(self): |
| 67 | """ |
| 68 | :return: Gets the name of the template, if a template has been loaded. |
| 69 | """ |
| 70 | return self.template["TemplateName"] if self.template is not None else None |
| 71 | |
| 72 | # snippet-start:[python.example_code.ses.CreateTemplate] |
| 73 | def create_template(self, name, subject, text, html): |
| 74 | """ |
| 75 | Creates an email template. |
| 76 | |
| 77 | :param name: The name of the template. |
| 78 | :param subject: The subject of the email. |
| 79 | :param text: The plain text version of the email. |
| 80 | :param html: The HTML version of the email. |
| 81 | """ |
no outgoing calls