我想將 SVG 內容(沒有保存的檔案)作為 HTML 發送到 Outlook 電子郵件地址。此 SVG 內容在顯示圓形影像的瀏覽器中運行良好,但通過 boto3.client 將其發送到 Outlook 電子郵件會導致郵件為空。為什么?任何建議表示贊賞。
import io
import json
import boto3
from botocore.exceptions import ClientError
SENDER = "Name1 LastName1 <[email protected]>"
RECIPIENT = "Name2 LastName2 <[email protected]>"
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'])
uj5u.com熱心網友回復:
SVG 圖形在 Web 瀏覽器中得到廣泛支持,但并非所有電子郵件程式都知道如何處理這些新影像。對于 Outlook,您需要將檔案匯出為任何影像檔案格式,例如 PNG 或 JPEG,然后將其附加到郵件專案。然后在訊息正文中,您可以參考使用帶有cid:
前綴的特殊語法。您還需要使用該方法設定PR_ATTACH_CONTENT_ID
MAPI 屬性(DASL 名稱“http://schemas.microsoft.com/mapi/proptag/0x3712001F”),并通過與附件上設定的值匹配的屬性參考該附件。對應訊息發送時的 MIME 標頭。Attachment.PropertyAccessor.SetProperty
src
PR_ATTACH_CONTENT_ID
PR_ATTACH_CONTENT_ID
Content-ID
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>"
uj5u.com熱心網友回復:
我找到了解決方案。使用send_raw_email
and MIMEImage
, cid
fromMIMEImage
用于在 html 正文中插入影像資料。更新代碼如下:
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import json
import boto3
from botocore.exceptions import ClientError
SENDER = "Name1 LastName1 <[email protected]>"
RECIPIENT = "Name2 LastName2 <[email protected]>"
AWS_REGION = "us-east-1"
SUBJECT = "something"
png_data = image_bytes()
CHARSET = "utf-8"
client = boto3.client('ses',region_name=AWS_REGION)
msg = MIMEMultipart('mixed')
msg['Subject'] = SUBJECT
msg['From'] = SENDER
msg['To'] = RECIPIENT
img = MIMEImage(png_data, 'png')
img.add_header("Content-Type", "image/png", name="some_name.png")
img.add_header("Content-Disposition", "inline", filename="some_name.png")
img.add_header('Content-Id', 'some_cid_name')
img.add_header('Content-Transfer-Encoding', 'base64')
BODY_HTML = '<img src="cid:some_cid_name"/>'
email_body= MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)
msg.attach(email_body)
try:
#Provide the contents of the email.
response = client.send_raw_email(
Source=SENDER,
Destinations=[
RECIPIENT
],
RawMessage={
'Data':msg.as_string(),
},
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['MessageId'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/475346.html