【问题标题】:html content is not sent in emailhtml 内容未通过电子邮件发送
【发布时间】:2017-03-20 03:11:59
【问题描述】:

我想将链接发送到电子邮件,因此当用户单击该链接时,用户将被重定向到推荐页面并可以推荐其他朋友。我使用 send_mail 发送电子邮件。除 html 消息外,所有内容都已发送。这是我所做的

  if created:
     new_join_old.invite_code = get_invite_code()
     new_join_old.save()
     subject = "Thank you for your request to sign up our community"
     html_message = '<a href="http://localhost:8000/{% url "invitations:refer-invitation" invite_code %}">Click Here</a>'
     message = "Welcome! We will be in contact with you."
     from_email = None
     to_email = [email]
     send_mail(subject, message, from_email, to_email, fail_silently=True, html_message=html_message)
     messages.success(request, '{0} has been invited'.format(email))
   return HttpResponseRedirect("/invitations/refer-invitation/%s"%(new_join_old.invite_code))
context = {"form": form}
return render(request, 'home.html', context)

【问题讨论】:

  • 如果您在本地以调试模式对其进行测试,您会在控制台中看到没有 HTML 部分的消息,对吧?
  • 你试过这个吗:html_message = '&lt;a href="http://localhost:8000{}"&gt;Click Here&lt;/a&gt;'.format(reverse('invitations:refer-invitation', kwrags={'invite_code': invite_code}))
  • 你得到什么错误?
  • nup 它不工作。仍然只有 html 消息没有发送

标签: python django django-1.9


【解决方案1】:

[更新]:为了使以下内容正常工作,您必须在 settings.py 文件中设置适当的 email settings,如下所示:

# settings.py

#######################
#   EMAIL SETTINGS    #
#######################
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'email_username_here'
EMAIL_HOST_PASSWORD = 'email_password_here'

&lt;a&gt; 链接未呈现,因为没有loader 来呈现它(未知的{% url %} 模板标签)。如果您想保留此语法 ({% url ... %}) 并有一个单独的 HTML 文件将被发送,则将该 HTML 文件存储为单独的文件,例如 html_email.html,然后使用 render_to_string 并执行以下操作:

<!-- html_email.html -->

<a href="http://localhost:8000/{% url 'invitations:refer-invitation' invite_code %}">Click Here</a>


# views.py

from django.template.loader import render_to_string

if created:
    # above code as is
    # in the context you can pass other context variables that will be available inside the html_email.html
    context = {'invite_code': new_join_old.invite_code,}
    html_message = render_to_string('path/to/html_email.html', context=context)
    # below code as is

或者你可以这样做:

#views.py

from django.core.mail import EmailMultiAlternatives
from django.urls import reverse

if created:
    # above code as is
    html_message = '<a href="http://localhost:8000{}">Click Here</a>'.format(reverse('invitations:refer-invitation', kwrags={'invite_code': invite_code}))
    msg = EmailMultiAlternatives(subject, message, from_email, to_email, fail_silently=True)
    msg.attach_alternative(html_message, 'text/html')
    msg.send()

【讨论】:

  • 我做了这个 already_join.save() subject = "感谢您请求注册我们的社区" context = {'invite_code': already_join.invite_code} html_message = render_to_string('email_templates/email. " html', context=context) message = "欢迎!我们将与您联系。" from_email = None to_email = [email] send_mail(subject, message, from_email, to_email, fail_silently=True, html_message=html_message) 但还没有工作。主题和消息已发送,但未发送 html_message
  • @pythonLover 使用必须设置的适当电子邮件设置更新了我的答案。你确定定了?因为我刚刚测试过它并且可以工作。
  • 抱歉回复晚了。我将芹菜用于异步任务队列。我已经用芹菜代码更新了我的问题,我正在使用 MAILGUN。
  • 我认为我的 EmailBackend 中缺少某些内容。它仍然无法正常工作。
  • EmailBackend__init__方法里面改成这个:super().__init__(self, **kwargs)
【解决方案2】:
from django.template import Context, Template

email_data = open('email_templates/email.html', 'r').read()
html_data = Template(email_data)
html_content = html_data.render(Context({'invite_code': new_join_old.invite_code}))

您也可以在上下文中添加messagesubject 等键值对

在 email.html 中

<!DOCTYPE html>
<html>

<head>
</head>
<body>
    <a href="http://localhost:8000/invitations/refer-invitation/{{ invite_code }}">Click Here</a>
</body>
</html>

您可以使用 {{ message }}{{ subject }} 在 email.html 中访问上下文变量的值,例如 messagesubject

https://docs.djangoproject.com/en/1.10/topics/email/#the-emailmessage-class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-23
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多