def test_domain_override(self):
     cid = attach_inline_image(self.message,
                               sample_image_content(),
                               domain="example.org")
     self.assertRegex(
         cid, r"[\w.]+@example\.org",
         "Content-ID should be a valid Message-ID @example.org")
 def test_default_domain(self, mock_getfqdn):
     """The default Content-ID domain should *not* use local hostname"""
     # (This avoids problems with ESPs that re-use Content-ID as attachment
     # filename: if the local hostname ends in ".com", you can end up with
     # an inline attachment filename that causes Gmail to reject the message.)
     mock_getfqdn.return_value = "server.example.com"
     cid = attach_inline_image(self.message, sample_image_content())
     self.assertRegex(
         cid, r"[\w.]+@inline", "Content-ID should be a valid Message-ID, "
         "but _not_ @server.example.com")
    def test_embedded_images(self):
        image_data = sample_image_content()  # Read from a png file

        cid = attach_inline_image(self.message, image_data)
        html_content = '<p>This has an <img src="cid:%s" alt="inline" /> image.</p>' % cid
        self.message.attach_alternative(html_content, "text/html")

        self.message.send()
        data = self.get_api_call_json()
        self.assertEqual(len(data['message']['images']), 1)
        self.assertEqual(data['message']['images'][0]["type"], "image/png")
        self.assertEqual(data['message']['images'][0]["name"], cid)
        self.assertEqual(decode_att(data['message']['images'][0]["content"]), image_data)
        # Make sure neither the html nor the inline image is treated as an attachment:
        self.assertFalse('attachments' in data['message'])
    def test_embedded_images(self):
        image_data = sample_image_content()  # Read from a png file

        cid = attach_inline_image(self.message, image_data)
        html_content = '<p>This has an <img src="cid:%s" alt="inline" /> image.</p>' % cid
        self.message.attach_alternative(html_content, "text/html")

        self.message.send()
        data = self.get_api_call_json()
        self.assertEqual(len(data['message']['images']), 1)
        self.assertEqual(data['message']['images'][0]["type"], "image/png")
        self.assertEqual(data['message']['images'][0]["name"], cid)
        self.assertEqual(decode_att(data['message']['images'][0]["content"]), image_data)
        # Make sure neither the html nor the inline image is treated as an attachment:
        self.assertFalse('attachments' in data['message'])
Example #5
0
def on_booking_create(sender, instance, created, *args, **kwargs):
    """Argument explanation:

       sender - The model class. (MyModel)
       instance - The actual instance being saved.
       created - Boolean; True if a new record was created.

       *args, **kwargs - Capture the unneeded `raw` and `using`(1.3) arguments.
    """
    if created:
        merge_data = {
            'PATIENT_NAME': instance.patient_name,
            'PATIENT_EMAIL': instance.patient_email,
            'APPOINTMENT_AT': instance.appointment_at,
            'FLIGHT_NUMBER': instance.flight_number,
            'FLIGHT_AIRLINE': instance.flight_airline,
            'FLIGHT_ORIGIN': instance.flight_origin,
        }

        qr = qrcode.QRCode(version=None,
                           image_factory=PymagingImage,
                           box_size=20,
                           border=4)
        qr.add_data(instance.uuid)
        image = qr.make_image(fit=True)
        image_bytes = io.BytesIO()
        image.save(image_bytes)

        subject = render_to_string("booking/conf_subject.txt",
                                   merge_data).strip()
        text_body = render_to_string("booking/conf_body.txt", merge_data)
        message = EmailMultiAlternatives(subject=subject,
                                         from_email="*****@*****.**",
                                         to=[instance.patient_email],
                                         body=text_body)
        merge_data["CID"] = attach_inline_image(message,
                                                image_bytes.getvalue(),
                                                "qr.png")
        html_body = render_to_string("booking/conf_body.html", merge_data)
        message.attach_alternative(html_body, "text/html")
        message.send()