簡體   English   中英

Django郵件不發送郵件

[英]Django mail not sending mail

我創建了一個函數,當重定向到特定 url 時,它將向特定用戶發送郵件。 它一直工作到今天。 但是,今天當它被重定向到 url 時,電子郵件顯示在終端中,而不是在收件人的收件箱中。 我在代碼中附加了郵件功能和設置。

視圖.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)

設置.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'
email_msg = EmailMessage(subject="subject",
                                     body="content",
                                     to=["xyz@gmail.com"])
email_msg.send()

使用電子郵件按摩

試試這個。 這對我有用。 您的 settings.py 文件似乎正確。

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

試試這個

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

作為內容,您可以根據需要添加字典或其他數據。 如果要發送模板,則可以使用

渲染到字符串

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM