简体   繁体   English

Django Mailgun API 返回 401 被禁止

[英]Django Mailgun API returning 401 forbidden

I've installed Django any mail and am I trying to use mail gun with it for password resets.我已经安装了 Django 任何邮件,我是否正在尝试使用邮枪来重置密码。 I've added any mail to my installed apps and tried to use mail gun as an API or smtp service.我已将任何邮件添加到已安装的应用程序中,并尝试将邮件枪用作 API 或 smtp 服务。 Both return back 401 forbidden两者都返回 401 禁止

For using mail guns API here's my code:对于使用邮枪 API 这是我的代码:

EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"
ANYMAIL_MAILGUN_API_KEY = config("MAIL_GUN_DOMAIN_API")

For MAIL_GUN_DOMAIN_API I tried using my accounts private key and I tried creating a domain and using the domains sending key.对于 MAIL_GUN_DOMAIN_API,我尝试使用我的帐户私钥,并尝试创建一个域并使用域发送密钥。 Both returned the same response.两者都返回了相同的响应。

for smtp:对于 smtp:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.mailgun.org'
EMAIL_PORT = 465
EMAIL_HOST_USER = config('MAIL_GUN_USERNAME')
EMAIL_HOST_PASSWORD = config('MAIL_GUN_SMTP_PASSWORD')
EMAIL_USE_TLS = True 

for MAIL_GUN_USERNAME I used my domains login for MAIL_GUN_SMTP_PASSWORD I used my domains password I tried all of mailguns different ports and switching between tls and ssl none of this worked.对于 MAIL_GUN_USERNAME 我使用我的域登录 MAIL_GUN_SMTP_PASSWORD 我使用我的域密码我尝试了所有 mailguns 的不同端口并在 tls 和 ssl 之间切换,这些都不起作用。

Any ideas what I'm doing wrong?任何想法我做错了什么?

The 401 error means your API key wasn't valid for the email you were trying to send. 401 错误意味着您的 API 密钥对于您尝试发送的 email 无效。 There are three likely possibilities:有三种可能的可能性:

  1. The From address for the emails you're sending doesn't match your Mailgun sending domain.您发送的电子邮件的发件人地址与您的 Mailgun 发送域不匹配。 Check that Django's DEFAULT_FROM_EMAIL is set to an address you're allowed to send from.检查 Django 的DEFAULT_FROM_EMAIL是否设置为您允许发送的地址。 (The default is "webmaster@localhost", which definitely won't work.) (默认是“webmaster@localhost”,肯定不行。)

    You might also want to look into Anymail's MAILGUN_SENDER_DOMAIN setting, depending on what emails you're trying to use and how your Mailgun account is set up.您可能还想查看 Anymail 的MAILGUN_SENDER_DOMAIN设置,具体取决于您尝试使用的电子邮件以及 Mailgun 帐户的设置方式。

  2. The API_KEY or PASSWORD you're reading from config() is incorrect.您从config()读取的 API_KEY 或 PASSWORD 不正确。 If you're getting your config from an.env file, a common mistake is trying to include a comment on the line with the key/password, which many versions of.env don't support.如果您从 .env 文件中获取配置,一个常见的错误是尝试在带有密钥/密码的行中包含注释,许多版本的 .env 都不支持。 This won't work:这不起作用:

     # In a.env file, comments should be on a separate line. MAIL_GUN_DOMAIN_API=mg-key-abcdefg # comment not allowed here!
  3. If you provisioned your sending domain in Mailgun's EU zone, you'll need to point Anymail to Mailgun's EU API servers instead of the default US servers.如果您在 Mailgun 的欧盟区域中配置了您的发送域,则需要将 Anymail 指向 Mailgun 的欧盟 API 服务器,而不是默认的美国服务器。 See Anymail's MAILGUN_API_URL setting.请参阅 Anymail 的MAILGUN_API_URL设置。 (I don't think this would affect SMTP, though.) (不过,我认为这不会影响 SMTP。)

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

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