Django mail not sending mail

笑着哭i 提交于 2019-12-02 16:02:48

问题


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

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'

回答1:


email_msg = EmailMessage(subject="subject",
                                     body="content",
                                     to=["xyz@gmail.com"])
email_msg.send()

use EmailMassage




回答2:


Try this one. This works for me. Your settings.py file seems correct.

from django.core.mail import EmailMessage

SUBJECT = "Welcome To my site"


def send_email(email, password):
    """ send email to new user with temp password"""

    msg = EmailMessage(SUBJECT, 'Your temporary login password here. {password}'.format(password=password), to=[email])
    msg.send()
    return True



回答3:


Try this one

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

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()


来源:https://stackoverflow.com/questions/56630798/django-mail-not-sending-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!