简体   繁体   English

无法在 django rest 框架中发送电子邮件

[英]Could not send email in django rest framework

I am developing password reset part of the project.我正在开发项目的密码重置部分。 I am using django_rest_passwordreset to get the password reset.我正在使用django_rest_passwordreset来重置密码。 I am using mailjet smtp.我正在使用mailjet smtp。 I could not send the email to the user.我无法将电子邮件发送给用户。

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'in-v3.mailjet.com'
# EMAIL_PORT = 465
EMAIL_PORT = 587
EMAIL_USE_TLS = True
# EMAIL_USE_SSL = True
EMAIL_HOST_USER = '5e4329460b3c88f1d24d19c3e7374548aa213da%asasd1asd'
EMAIL_HOST_PASSWORD = 'a6c5ab2515d6ae761253a396453530ba$42asasdasdaasdasd'

If I change the EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' to the EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' it is printing it to the console.如果我将EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'更改为EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'它会将其打印到控制台。 I have no idea why it is not working.我不知道为什么它不起作用。

the part of the code where I am trying to send an email.我试图发送电子邮件的代码部分。

@receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
    # send an e-mail to the user
    context = {
        'current_user': reset_password_token.user,
        'username': reset_password_token.user.firstname,
        'email': reset_password_token.user.email,
        'reset_password_url': "{}?token={}".format(reverse('password_reset:reset-password-request'), reset_password_token.key)
    }
    # just checking if it works
    send_mail('Hello from something', 'hello there', 'abdukhashimov@yandex.ru',
              [reset_password_token.user.email, ], fail_silently=False)
    # render email text
    email_html_message = render_to_string('user_reset_password.html', context)
    email_plaintext_message = render_to_string(
        'user_reset_password.txt', context)

    msg = EmailMultiAlternatives(
        # title:
        "Password Reset for {title}".format(title="Some website title"),
        # message:
        email_plaintext_message,
        # from:
        "noreply@somehost.local",
        # to:
        [reset_password_token.user.email]
    )
    msg.attach_alternative(email_html_message, "text/html")
    msg.send()

I came up with a different solution.我想出了一个不同的解决方案。 I used the google smtp service.我使用了谷歌 smtp 服务。 I have followed the steps from this kinsta.com - steps to setup up google smtp .我已按照此kinsta.com 中的步骤 - 设置 google smtp 的步骤

Step1: The very first thing you will need to do is ensure that you have 2-step verification enabled on your primary Gmail account.第 1 步您需要做的第一件事是确保您的主 Gmail 帐户启用了两步验证。 Important: If you don't do this you will get an invalid password error further below when trying to authenticate your email address.重要提示:如果您不这样做,您将在下面尝试验证您的电子邮件地址时收到无效密码错误。 So first go and enable 2-step verification.所以首先去启用两步验证。

Step2: Next, you will need to generate an App password. Step2:接下来,您需要生成一个应用密码。 You then use the app password in place of your personal Gmail password further below.然后,您可以在下方使用应用密码代替您的个人 Gmail 密码。 This is the only way this process will work.这是此过程将起作用的唯一方式。

Step3: Now back in Gmail, go to settings, and can click on “Accounts and Import.”第三步:现在回到 Gmail,进入设置,然后点击“帐户和导入”。 Then click on “Add another email address you own.”.然后单击“添加您拥有的另一个电子邮件地址。”。 Basically to the gmail and sign in with your account and go to the settings.基本上到gmail并使用您的帐户登录并转到设置。

Step4: Enter your additional business name and business email that is on the custom domain.第 4 步:输入自定义域中的其他企业名称和企业电子邮件。

来自 kinsta.com 的图片显示了如何添加另一个电子邮件帐户 (Extra Info). (额外信息)。 I usually use yandex mail and added it then it generated the followings.我通常使用 yandex 邮件并添加它然后它生成了以下内容。

我生成的信息

Step5: It will then send an email confirmation code to the email you just added.步骤5:然后它会向您刚刚添加的电子邮件发送电子邮件确认码。 You will need to click the link in the email to confirm it or manually enter the code (this proves that you are in fact the owner of the additional email account).您需要单击电子邮件中的链接进行确认或手动输入代码(这证明您实际上是附加电子邮件帐户的所有者)。 And that's it!就是这样!

From my experience, you might need to tweak some settings from google if it did not work for you.根据我的经验,如果它对您不起作用,您可能需要从 google 调整一些设置。 For example, I read from other source, you might need to allow less secure apps from google.例如,我从其他来源阅读,您可能需要允许来自谷歌的安全性较低的应用程序。 I have not done it as I was using yandex mail, I guess.我没有这样做,因为我使用的是 yandex 邮件,我猜。

In case if you are not sure what to put in settings.py如果您不确定在settings.py放什么

EMAIL_HOST = 'smtp.yandex.ru' # in my case
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'added account'
EMAIL_HOST_PASSWORD = 'your password'

Credits to kinsta.com归功于 kinsta.com

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

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