为什么我的django电子邮件的html链接无法点击?

时间:2011-06-24 07:31:48

标签: python html django email hyperlink

我通过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()

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

谢谢!

2 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:1)

我构建了一个辅助函数来执行此操作,并使用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)