我正在开发项目的密码重置部分。我正在使用django_rest_passwordreset
重置密码。我正在使用mailjet smtp。我无法将电子邮件发送给用户。
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'
如果将EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
更改为EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
,则会将其打印到控制台。我不知道为什么它不起作用。
我尝试发送电子邮件的代码部分。
@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', '[email protected]',
[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:
"[email protected]",
# to:
[reset_password_token.user.email]
)
msg.attach_alternative(email_html_message, "text/html")
msg.send()
我想出了一个不同的解决方案。我使用了Google SMTP服务。我从[kinsta.com - steps to setup up google smtp]开始执行步骤。
[Step1:首先,您需要做的是确保在主Gmail帐户上启用了两步验证。重要提示:如果您不这样做,则在尝试验证电子邮件地址时,您将在下面进一步看到无效的密码错误。因此,请首先启用两步验证。
Step2:接下来,您需要生成一个应用密码。然后,您可以在下方使用应用密码代替您的个人Gmail密码。这是此过程将起作用的唯一方法。
[Step3:现在返回Gmail,转到设置,然后单击“帐户和导入”。然后单击“添加您拥有的另一个电子邮件地址。”。基本上是gmail,并使用您的帐户登录并转到设置。
Step4:输入自定义域上的其他公司名称和公司电子邮件。
(更多信息)。我通常使用yandex邮件并将其添加,然后生成以下邮件。
Step5:然后,它将向您刚刚添加的电子邮件发送电子邮件确认代码。您将需要单击电子邮件中的链接进行确认或手动输入代码(这证明您实际上是其他电子邮件帐户的所有者)。就这样!
根据我的经验,如果它不适合您,则可能需要从Google调整一些设置。例如,我从其他来源读到的内容,您可能需要允许不太安全的Google应用。我想我没有像使用yandex邮件那样做。
[如果您不确定要放入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'
[C0的积分]