简体   繁体   English

使用 Mailgun API 通过 Python 发送电子邮件

[英]Sent Email via Python using Mailgun API

I try to send email via python using Mailgun我尝试使用 Mailgun 通过 python 发送电子邮件

I have我有

def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org",
        auth=("api", "key-********"),
        data={"from": "Excited User <bb@gmail.com>",
              "to": ["bb@outlook.com", "bb4@gmail.com"],
              "subject": "Hello",
              "text": "Testing some Mailgun awesomness!"})

I realize I forgot /messages我意识到我忘记了/messages

def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org/messages",
        auth=("api", "key-********"),
        data={"from": "Excited User <bb@gmail.com>",
              "to": ["bb@outlook.com", "bb4@gmail.com"],
              "subject": "Hello",
              "text": "Testing some Mailgun awesomness!"})

I run it我运行它

python mail.py 

I still don't see any email.我仍然没有看到任何电子邮件。 I also check my MailGun and my spam folder.我还检查了我的 MailGun 和我的垃圾邮件文件夹。

Any hints for me ?对我有什么提示吗?

Avoiding to the MailGun documentation , here you can see that the POST url should be in this format https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages 避免使用MailGun文档 ,在这里您可以看到POST网址应采用以下格式https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages //api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages

From your piece of code, I can see that sandbox6247218655a94010b9840c23c2688fc7.mailgun.org is YOUR_DOMAIN_NAME but your missing the format which is the /messages API endpoint. 从您的代码中,我可以看到sandbox6247218655a94010b9840c23c2688fc7.mailgun.orgYOUR_DOMAIN_NAME但是您缺少/messages API端点的格式。

So all you have to do is to add the /messages endpoint to your post URL. 因此,您要做的就是将/messages端点添加到您的帖子URL。 So it changes from https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org to https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org/messages . 因此它从https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org更改为https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org/messages

Update: I skipped that part, yes you don't see any mail from your inbox due to the fact that you're trying to send the email on behalf of GMail. 更新:我跳过了这一部分,是的,由于您正尝试代表GMail发送电子邮件,因此您看不到收件箱中的任何邮件。 You can send emails on behalf of and ONLY from the domain you've registered. 您可以代表注册的域发送电子邮件,并且只能从注册的域发送电子邮件。

Thats it! 而已!

Also note that if your domain is added to the EU region (instead of US) you have to change the post URL domain from api.mailgun.net to api.eu.mailgun.net (note the .eu ), as read in the documentation .另请注意,如果您的域被添加到欧盟地区(而不是美国),您必须将 post URL 域从api.mailgun.netapi.eu.mailgun.net (注意.eu ),如阅读文档

So the entire URL would be https://api.eu.mailgun.net/v3/YOUR_DOMAIN_NAME/messages所以整个 URL 将是https://api.eu.mailgun.net/v3/YOUR_DOMAIN_NAME/messages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM