我创建了一个函数,当重定向到特定的URL时,它将向特定的用户发送邮件。一直工作到今天。但是,今天,当将其重定向到url时,该电子邮件将显示在终端中,而不是显示在收件人的收件箱中。我在代码中附加了邮件功能和设置。
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'
答案 0 :(得分:0)
email_msg = EmailMessage(subject="subject",
body="content",
to=["xyz@gmail.com"])
email_msg.send()
使用EmailMassage
答案 1 :(得分: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
答案 2 :(得分:0)
尝试这个
$data
作为内容,您可以添加所需的字典或其他数据。 如果要发送模板,则可以使用
render_to_string
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()