【问题标题】:Mailgun inline image utf-8 pngMailgun 内联图像 utf-8 png
【发布时间】:2019-08-06 12:20:30
【问题描述】:

我正在尝试从 Mailgun 获取内联图像示例以使用 Python,但由于某种原因,当我添加 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 图像一起使用?

【问题讨论】:

    标签: python png inline mailgun


    【解决方案1】:

    尝试在您的 open() 调用中添加“rb”:

    files=[("inline", open("files/test.png", 'rb'))]

    【讨论】:

    • 图片路径正确。 open("files/test.png").show() 抛出错误 AttributeError: '_io.TextIOWrapper' object has no attribute 'show' open 命令仅在请求中失败。如果它在函数之外运行,它可以工作。我目前没有使用图像库。如果需要,您能否更详细地解释您的答案,说明为什么这将与 .png、Requests 和 Mailgun 示例一起使用。我用 .jpg 测试了这个函数,它可以工作,但不是 .png
    • 抱歉,它也不适用于 .jpg。两种格式都抛出错误。
    • 我尝试在没有图像库的情况下仅使用 'rb' 并且它有效。例如。 files=[("inline", open("files/test.png", 'rb'))]如果你想编辑你的答案,我会接受,这样其他用户就清楚了。
    猜你喜欢
    • 1970-01-01
    • 2016-12-10
    • 2014-10-02
    • 2014-11-27
    • 1970-01-01
    • 2014-07-27
    • 2019-07-18
    • 1970-01-01
    • 2016-10-11
    相关资源
    最近更新 更多