2011-06-24 55 views
3

我通過Django的爲什麼我的django電子郵件的html鏈接無法點擊?

subject, from_email, to = 'site Registration', '[email protected]', 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() 

發送以下電子郵件當我去的電子郵件(谷歌和雅虎的電子郵件),他們都顯示的點擊此處以藍色文本(我想這意味着它認爲是一個鏈接),但是,該鏈接不可點擊,並且不會鏈接到site.com/acc ...我做錯了什麼?

謝謝!

回答

6

你可能需要做一個有效的URL:(注意列入計劃)

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

真棒THX一堆!!!!! – Derek

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) 
相關問題