我正在尝试使用Python使Mailgun的内联图像示例工作,但是当我添加PNG图像时,会抛出Unicode错误。以下是我使用的示例函数,并将jpg更改为png。
def send_inline_image():
return requests.post(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
auth=("api", "YOUR_API_KEY"),
files=[("inline", open("files/test.png"))],
data={"from": "Excited User <YOU@YOUR_DOMAIN_NAME>",
"to": "bar@example.com",
"subject": "Hello",
"text": "Testing some Mailgun awesomness!",
"html": '<html>Inline image here: <img src="cid:test.png"></html>'})
这是抛出的异常:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
没有内联图片时,一切都按预期工作。我该如何使用 PNG 图像使其正常工作?
open("files/test.png").show()
报错AttributeError: '_io.TextIOWrapper' object has no attribute 'show'
。打开命令只在请求内失败。如果在函数外运行,它可以正常工作。我目前没有使用Image库。如果需要,请详细说明为什么这可以用于.png、请求和Mailgun示例。我测试了函数的.jpg格式,它可以正常工作,但不能使用.png格式。 - Mattsfiles=[("inline", open("files/test.png", 'rb'))]
如果您想编辑您的答案,我会接受它,这样其他用户就清楚了。 - Matts