コード例 #1
0
    def test_template(self):
        message = AnymailMessage(
            template_id=5,  # There is a *new-style* template with this id in the Anymail test account
            from_email='Sender <*****@*****.**>',  # Override template sender
            to=["Recipient <*****@*****.**>"],  # No batch send (so max one recipient suggested)
            reply_to=["Do not reply <*****@*****.**>"],
            tags=["using-template"],
            headers={"X-Anymail-Test": "group: A, variation: C"},
            merge_global_data={
                # The Anymail test template includes `{{ params.SHIP_DATE }}`
                # and `{{ params.ORDER_ID }}` substitutions
                "SHIP_DATE": "yesterday",
                "ORDER_ID": "12345",
            },
            metadata={"customer-id": "ZXK9123", "meta2": 2},
        )

        # Normal attachments don't work with Sendinblue templates:
        #   message.attach("attachment1.txt", "Here is some\ntext for you", "text/plain")
        # If you can host the attachment content on some publicly-accessible URL,
        # this *non-portable* alternative allows sending attachments with templates:
        message.esp_extra = {
            'attachment': [{
                'name': 'attachment1.txt',
                # URL where Sendinblue can download the attachment content while sending:
                'url': 'https://raw.githubusercontent.com/anymail/django-anymail/master/AUTHORS.txt',
            }]
        }

        message.send()
        self.assertEqual(message.anymail_status.status, {'queued'})  # SendinBlue always queues
        self.assertRegex(message.anymail_status.message_id, r'\<.+@.+\>')
コード例 #2
0
ファイル: api.py プロジェクト: mitodl/mitxpro
def build_message(connection,
                  template_name,
                  recipient,
                  context,
                  metadata=None):
    """
    Creates a message object

    Args:
        connection: An instance of the email backend class (return value of django.core.mail.get_connection)
        template_name (str): name of the template, this should match a directory in mail/templates
        recipient (str): Recipient email address
        context (dict or None): A dict of context variables
        metadata (EmailMetadata or None): An object containing extra data to attach to the message

    Returns:
        django.core.mail.EmailMultiAlternatives: email message with rendered content
    """
    subject, text_body, html_body = render_email_templates(
        template_name, context or {})
    msg = AnymailMessage(
        subject=subject,
        body=text_body,
        to=[recipient],
        from_email=settings.MAILGUN_FROM_EMAIL,
        connection=connection,
        headers={"Reply-To": settings.MITXPRO_REPLY_TO_ADDRESS},
    )
    esp_extra = {}
    if metadata:
        if metadata.tags:
            esp_extra.update({"o:tag": metadata.tags})
        if metadata.user_variables:
            esp_extra.update({
                "v:{}".format(k): v
                for k, v in metadata.user_variables.items()
            })
    if esp_extra:
        msg.esp_extra = esp_extra
    msg.attach_alternative(html_body, "text/html")
    return msg