Send SVG image content to email as HTML using boto3

I want to send SVG content (without saved file) as HTML to Outlook email address. This SVG content works fine in browser showing a circle image, however sending it via boto3.client to Outlook email results in empty mail. Why? Any suggestions appreciated.

import io
import json
import boto3
from botocore.exceptions import ClientError

SENDER = "Name1 LastName1 <Name1.LastName1@mail.com>"
RECIPIENT = "Name2 LastName2 <Name2.LastName2@mail.com>"
AWS_REGION = "us-east-1"
SUBJECT = "something"

svg = """
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg> 
"""

BODY_HTML = f"""<html>
<body>
{svg}
</body>
</html>
            """ 

CHARSET = "UTF-8"

client = boto3.client('ses',region_name=AWS_REGION)

try:
    response = client.send_email(
        Destination={
            'ToAddresses': [
                RECIPIENT
            ],
        },
        Message={
            'Body': {
                'Html': {
                    'Charset': CHARSET,
                    'Data': BODY_HTML
                }
            },
            'Subject': {
                'Charset': CHARSET,
                'Data': SUBJECT
            },
        },
        Source=SENDER
    )   
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID:"),
    print(response['MessageId'])

1 answer

  • answered 2022-05-07 19:32 Eugene Astafiev

    SVG graphics are widely supported in web browsers but not all email programs know what to do with these new images. In case of Outlook you need to export the file to any image file format like PNG or JPEG and then attach it to the mail item. Then in the message body you can refer to use using a special syntax with a cid: prefix. Also you need to set the PR_ATTACH_CONTENT_ID MAPI property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using the Attachment.PropertyAccessor.SetProperty method and refer that attachment through the src attribute that matches the value of PR_ATTACH_CONTENT_ID set on the attachment. PR_ATTACH_CONTENT_ID corresponds to the Content-ID MIME header when the message is sent.

    attachment = MailItem.Attachments.Add("c:\temp\MyPicture.jpg")
    attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "YourImageId")
    MailItem.HTMLBody = "<html><body>Test image <img src=""cid:YourImageId""></body></html>"
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum