Django邮件未发送邮件 [英] Django mail not sending mail

查看:90
本文介绍了Django邮件未发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数,当重定向到特定的URL时,它将向特定的用户发送邮件。一直工作到今天。但是,今天,当将其重定向到url时,该电子邮件将显示在终端中,而不是显示在收件人的收件箱中。我在代码中附加了邮件功能和设置。

I have created a function the would send mail to a particular user when redirected to a particular url. It was working until today. However, today when it gets redirected to the url, the email is displayed in the terminal and not in the inbox of reciever. I am attaching the mail function and settings in the code.

views.py

def mail(request,pk):
    pr = UserProfile.objects.get(pk=pk)
    subject = "Greetings"
    msg = "Congratulations for your success"
    to = 'urvi0728@gmail.com'
    res = send_mail(subject, msg, settings.EMAIL_HOST_USER, [to])
    if(res == 1):
        msg = "Mail Sent Successfuly"
    else:
        msg = "Mail could not be sent"
    return HttpResponse(msg)

settings.py

settings.py

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '*****@gmail.com'
EMAIL_HOST_PASSWORD = '*********'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'


推荐答案

试试这个

from django.core.mail import EmailMessage 

to_email = your_user_email
mail_subject = your_message_subject
message = your_content
send_message = EmailMessage(mail_subject, message, to=[to_email])
send_message.content_subtype = "html"
send_message.send()

作为内容,您可以添加所需的字典或其他数据。
如果要发送模板,则可以使用

As content you can add dictionaries or other data what you wish. If you want to send a template then you can use


render_to_string

render_to_string



from django.template.loader import render_to_string

message = render_to_string('path_to_your_template.html', {
        'domain':current_site,
        'email': request.user.email,
        'data' : your_data
    })
send_message = EmailMessage(mail_subject, message, to=[to_email])
send_message.content_subtype = "html"
send_message.send()

这篇关于Django邮件未发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆