0

我创建了一个函数,当重定向到特定 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'
4

3 回答 3

0

试试这个

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()
于 2019-06-17T13:05:40.920 回答
0

试试这个。这对我有用。您的 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
于 2019-06-17T12:20:16.893 回答
0
email_msg = EmailMessage(subject="subject",
                                     body="content",
                                     to=["xyz@gmail.com"])
email_msg.send()

使用电子邮件按摩

于 2019-06-17T12:11:20.693 回答