我遇到了一个在Django上发送电子邮件的问题。我认为我的设置,视图和模型是正确的,但尽管该邮件是不发送。我在控制台上试这个没有问题
Django version == 4.2.1 Python version == 3.10
网址:settings.py
`EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = 'smtp.mail.ru'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'storedjango@mail.ru'
EMAIL_HOST_PASSWORD = '*****'
EMAIL_USE_SSL = True`
字符串
网址:models.py
`class EmailVerification(models.Model):
code = models.UUIDField(unique=True)
user = models.ForeignKey(to=User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
expiration = models.DateTimeField()
def __str__(self):
return f'Email verification for user {self.user.email}'
def send_verification_email(self):
link = reverse('users:email_verification', kwargs={'email': self.user.email, 'code': self.code})
verification_link = f'{settings.DOMAIN_NAME}{link}'
subject = f'Verification for {self.user.username}'
message = f'Click to the link to verify your account {self.user.email} {verification_link} '
send_mail(
subject=subject,
message=message,
from_email=settings.EMAIL_HOST_USER,
recipient_list=[self.user.email],
fail_silently=False
)
def is_expired(self):
return True if now() >= self.expiration else False`
型
网址:views.py
`class EmailVerificationView(TemplateView):
template_name = 'users/email_verification.html'
def get(self, request, *args, **kwargs):
code = kwargs['code']
user = User.objects.get(email=kwargs['email'])
email_verifications = EmailVerification.objects.filter(user=user, code=code)
if email_verifications.exists() and not email_verifications.first().is_expired():
user.is_verified_email = True
user.save()
return super(EmailVerificationView, self).get(request, *args, **kwargs)
else:
return HttpResponseRedirect(reverse('index'))`
型
网址:forms.py
` def save(self, commit=True):
user = super(UserRegistrationForm, self).save(commit=True)
expiration = now() + timedelta(hours=48)
record = EmailVerification.objects.create(code=uuid.uuid4(), user=user, expiration=expiration)`
型
这个问题的核心原因是什么?有什么解决办法吗?谢啦,谢啦
1条答案
按热度按时间wlwcrazw1#
尝试从发送邮件打印结果并检查错误消息。
字符串