【问题标题】:Why are my html links from django email not clickable?为什么我的 django 电子邮件中的 html 链接不可点击?
【发布时间】:2011-09-21 19:49:27
【问题描述】:

我通过 Django 发送了以下电子邮件

    subject, from_email, to = 'site Registration', 'support@site.com', self.second_email
    text_content = 'Click on link to finish registration'
    html_content = '<html><body><a href="site.com/accounts/user/' + self.slug +'">Click Here</a></body></html>'
    msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()

当我发送电子邮件(google 和 yahoo 电子邮件)时,它们都以蓝色显示 Click Here 文本(我认为这意味着它被识别为链接),但是,该链接不可点击,也未链接到站点.com/acc...我做错了什么?

谢谢!

【问题讨论】:

    标签: python html django email hyperlink


    【解决方案1】:

    您可能需要将其设为有效 URL:(注意包含的方案)

    <a href="http://site.com/accounts/user/' + self.slug +'">
    

    【讨论】:

      【解决方案2】:

      我构建了一个辅助函数来执行此操作,并使用render_to_string 使用 HTML 模板发送而不是在函数中输入所有内容。这是我的功能:

      def signup_email(username, email, signup_link):
          subject, from_email, to = 'Site Registration', settings.DEFAULT_FROM_EMAIL, email
          text_content = 'Click on link to finish registration'
          html_content = render_to_string('pet/html_email.html', {'username': username, 'signup_link':signup_link})
          msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
          msg.attach_alternative(html_content, "text/html")
          return msg 
      

      然后在views.py中调用它:

      class OwnerCreateAccount(FormView):
          # class based view  attributes
      
          def form_valid(self, form):
              # other form logic
              msg = signup_email(username=username, email=email, signup_link=owner.salt)
              msg.send()
              return super(OwnerCreateAccount, self).form_valid(form)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 2018-10-18
        • 2013-11-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2021-11-10
        相关资源
        最近更新 更多