Mailgun API 因未知原因拒绝邮件

Mailgun API rejecting messages for unknown reason

我运行发送HTML邮件的小测试(三封邮件)。所有三个都被拒绝 (400),原因如下:

{https://api.mailgun.net:443 "POST /v3/mg001.[mydomain].com/messages.mime HTTP/1.1" 400 0}

我真的不知道它在告诉我什么。

"sends" 电子邮件的 python 代码是:

def send_message(sender, subject, body_text, body_html, recipient):
    return requests.post(
        "https://api.mailgun.net/v3/mg001.[mydomain].com/messages.mime",
        auth=("api", "[myapikey]"),
        data={"from": sender,
              "to": recipient,
              "subject": subject,
              "text": body_text,
              "html": body_html},
        headers={"Content-Type": "multipart/form-data"}
        )

我的代码略有不同,可以完美地发送非 html 电子邮件,所以我知道我的帐户或 API 密钥等没有任何问题

如果有人能指出正确的方向,我将不胜感激。

The docs say:

POST /<domain>/messages.mime

Posts a message in MIME format. Note: you will need to build a MIME string yourself. Use a MIME library for your programming language to do this. Pass the resulting MIME string as message parameter.

AFAICT 你没有构建 MIME 字符串,所以你不应该使用那个端点。 The quickstart 显示要使用的标准端点:

https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages

The examples in of the docs 显示了一个发送 HTML 消息的非常清晰的示例(它包括文件附件,我已将其删除):

send_complex_message():
    return requests.post(
        "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
        auth=("api", "YOUR_API_KEY"),
        data={"from": "Excited User <YOU@YOUR_DOMAIN_NAME>",
              "to": "foo@example.com",
              "cc": "baz@example.com",
              "bcc": "bar@example.com",
              "subject": "Hello",
              "text": "Testing some Mailgun awesomness!",
              "html": "<html>HTML version of the body</html>"})

None 的参考示例包括使用 Content-Type header,它看起来不像是必需的。

TL;DR - 如有疑问,请查看文档! :-)