问题描述
我正在使用 Mailjet 并尝试将图像插入到以编程方式生成的电子邮件正文中。不幸的是,我没有看到正文中的图像,这是我测试时得到的结果:
我一直在处理文档中的 this example 和 tried this solution from a GitHub issue,但没有成功。
我的代码如下:
import os
from mailjet_rest import Client
import numpy as np
import plotly.graph_objects as go
import base64
MAILJET_KEY = os.environ.get("MAILJET_KEY")
MAILJET_SECRET = os.environ.get("MAILJET_SECRET")
mailjet = Client(auth=(MAILJET_KEY,MAILJET_SECRET),version='v3.1')
filename = 'sample1.png'
# Plotly code taken from: https://plotly.com/python/static-image-export/
N = 100
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sz = np.random.rand(N) * 30
fig = go.figure()
fig.add_trace(go.Scatter(
x=x,y=y,mode="markers",marker=go.scatter.Marker(
size=sz,color=colors,opacity=0.6,colorscale="Viridis"
)
))
# Save image
fig.write_image(filename)
# Base64 Encode
with open(filename,'rb') as file:
encode64 = base64.b64encode(file.read())
cid = "id1"
data = {
'Messages': [
{
"From": {
"Email": "myemail@mydomain.com","Name": "Mailjet API Image Test"
},"To": [{
"Email": "myemail@gmail.com","Name": "My Name"
}],"Subject": "Can I insert an image inline?","TextPart": "Here's a random plot for your enjoyment!","HTMLPart": ("<h3>We're putting an image inline:<br/>" +
f"<img src=\"cid:{cid}\">" +
"<br/>Other text below."),"InlinedAttachments": [{
"ContentType": "image/png","Filename": filename,"ContentID": cid,"Base64Content": str(encode64)
}]
}
]
}
result = mailjet.send.create(data=data)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)