import os import unittest import warnings from email.utils import formataddr from django.test import SimpleTestCase, override_settings, tag from anymail.exceptions import AnymailAPIError from anymail.message import AnymailMessage from .utils import AnymailTestMixin, sample_image_path ANYMAIL_TEST_AMAZON_SES_ACCESS_KEY_ID = os.getenv("ANYMAIL_TEST_AMAZON_SES_ACCESS_KEY_ID") ANYMAIL_TEST_AMAZON_SES_SECRET_ACCESS_KEY = os.getenv("ANYMAIL_TEST_AMAZON_SES_SECRET_ACCESS_KEY") ANYMAIL_TEST_AMAZON_SES_REGION_NAME = os.getenv("ANYMAIL_TEST_AMAZON_SES_REGION_NAME", "us-east-1") ANYMAIL_TEST_AMAZON_SES_DOMAIN = os.getenv("ANYMAIL_TEST_AMAZON_SES_DOMAIN") @unittest.skipUnless( ANYMAIL_TEST_AMAZON_SES_ACCESS_KEY_ID and ANYMAIL_TEST_AMAZON_SES_SECRET_ACCESS_KEY and ANYMAIL_TEST_AMAZON_SES_DOMAIN, "Set ANYMAIL_TEST_AMAZON_SES_ACCESS_KEY_ID and ANYMAIL_TEST_AMAZON_SES_SECRET_ACCESS_KEY " "and ANYMAIL_TEST_AMAZON_SES_DOMAIN environment variables to run Amazon SES integration tests") @override_settings( EMAIL_BACKEND="anymail.backends.amazon_ses.EmailBackend", ANYMAIL={ "AMAZON_SES_CLIENT_PARAMS": { # This setting provides Anymail-specific AWS credentials to boto3.client(), # overriding any credentials in the environment or boto config. It's often # *not* the best approach -- see the Anymail and boto3 docs for other options. "aws_access_key_id": ANYMAIL_TEST_AMAZON_SES_ACCESS_KEY_ID, "aws_secret_access_key": ANYMAIL_TEST_AMAZON_SES_SECRET_ACCESS_KEY, "region_name": ANYMAIL_TEST_AMAZON_SES_REGION_NAME, # Can supply any other boto3.client params, including botocore.config.Config as dict "config": {"retries": {"max_attempts": 2}}, }, "AMAZON_SES_CONFIGURATION_SET_NAME": "TestConfigurationSet", # actual config set in Anymail test account }) @tag('amazon_ses', 'live') class AmazonSESBackendIntegrationTests(AnymailTestMixin, SimpleTestCase): """Amazon SES API integration tests These tests run against the **live** Amazon SES API, using the environment variables `ANYMAIL_TEST_AMAZON_SES_ACCESS_KEY_ID` and `ANYMAIL_TEST_AMAZON_SES_SECRET_ACCESS_KEY` as AWS credentials. If those variables are not set, these tests won't run. (You can also set the environment variable `ANYMAIL_TEST_AMAZON_SES_REGION_NAME` to test SES using a region other than the default "us-east-1".) Amazon SES doesn't offer a test mode -- it tries to send everything you ask. To avoid stacking up a pile of undeliverable @example.com emails, the tests use Amazon's @simulator.amazonses.com addresses. https://docs.aws.amazon.com/ses/latest/DeveloperGuide/mailbox-simulator.html """ def setUp(self): super().setUp() self.from_email = 'test@%s' % ANYMAIL_TEST_AMAZON_SES_DOMAIN self.message = AnymailMessage('Anymail Amazon SES integration test', 'Text content', self.from_email, ['success@simulator.amazonses.com']) self.message.attach_alternative('
HTML content
', "text/html") # boto3 relies on GC to close connections. Python 3 warns about unclosed ssl.SSLSocket during cleanup. # We don't care. (It may be a false positive, or it may be a botocore problem, but it's not *our* problem.) # https://github.com/boto/boto3/issues/454#issuecomment-586033745 # Filter in TestCase.setUp because unittest resets the warning filters for each test. # https://stackoverflow.com/a/26620811/647002 warnings.filterwarnings("ignore", message=r"unclosedHTML: with link"
"and image: " % cid,
"text/html")
message.attach_alternative(
"Amazon SES SendRawEmail actually supports multiple alternative parts",
"text/x-note-for-email-geeks")
message.send()
self.assertEqual(message.anymail_status.status, {'queued'})
def test_stored_template(self):
# Using a template created like this:
# boto3.client('ses').create_template(Template={
# "TemplateName": "TestTemplate",
# "SubjectPart": "Your order {{order}} shipped",
# "HtmlPart": "
Your order {{order}} shipped {{ship_date}}.
", # "TextPart": "Dear {{name}}:\r\nYour order {{order}} shipped {{ship_date}}." # }) message = AnymailMessage( template_id='TestTemplate', from_email=formataddr(("Test From", self.from_email)), to=["First Recipient