2009-08-25 115 views
11

即時通訊使用django註冊,一切正常,確認電子郵件以純文本形式發送,但知道即時通訊固定並正在發送html,但我有一個垃圾問題...在HTML代碼顯示:django +用django註冊html發送電子郵件

<a href="http://www.example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/">http://www. example.com/accounts/activate/46656b86eefc490baf4170134429d83068642139/</a> 

和我不需要證明,如...

任何想法的HTML代碼?

謝謝

回答

14

我建議發送文本版本和html版本。看看在Django的登記models.py爲:

send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email]) 

,而是做一些像是從文檔http://docs.djangoproject.com/en/dev/topics/email/#sending-alternative-content-types

from django.core.mail import EmailMultiAlternatives 

subject, from_email, to = 'hello', '[email protected]', '[email protected]' 
text_content = 'This is an important message.' 
html_content = '<p>This is an <strong>important</strong> message.</p>' 
msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) 
msg.attach_alternative(html_content, "text/html") 
msg.send() 
+0

是保羅,thans重播,但不工作我做了那樣的並沒有什麼......但現在是確定的:),現在只是把鏈接,而不<一... – Asinox 2009-09-12 16:44:16

+0

這將發送一個文本電子郵件,一些客戶將創建鏈接。如果你需要更有趣的html,你將不得不做我推薦的。 – 2009-09-12 21:25:41

+0

是的,我嘗試過,但不工作,但沒關係:)生病嘗試更多的垃圾:) – Asinox 2009-09-13 15:16:41

27

爲了避免修補Django的註冊,你應該繼承與RegistrationProfile模型proxy=True

models.py

class HtmlRegistrationProfile(RegistrationProfile): 
    class Meta: 
     proxy = True 
    def send_activation_email(self, site): 
     """Send the activation mail""" 
     from django.core.mail import EmailMultiAlternatives 
     from django.template.loader import render_to_string 

     ctx_dict = {'activation_key': self.activation_key, 
        'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS, 
        'site': site} 
     subject = render_to_string('registration/activation_email_subject.txt', 
            ctx_dict) 
     # Email subject *must not* contain newlines 
     subject = ''.join(subject.splitlines()) 

     message_text = render_to_string('registration/activation_email.txt', ctx_dict) 
     message_html = render_to_string('registration/activation_email.html', ctx_dict) 

     msg = EmailMultiAlternatives(subject, message_text, settings.DEFAULT_FROM_EMAIL, [self.user.email]) 
     msg.attach_alternative(message_html, "text/html") 
     msg.send() 

而在您的註冊後端,只需使用HtmlRegistrationProfile而不是註冊配置文件

+0

這是要走的路。工作很好。 – ajt 2011-09-25 13:05:29

+0

我如何註冊註冊後端的新配置文件? – Sam 2011-11-15 09:57:15

+10

如何將後端設置爲HtmlRegistration配置文件而不是RegistrationProfile? – AlexBrand 2012-05-07 14:19:58

2

我知道這是老註冊軟件包不再維持。以防萬一有人還想要這個。 額外的步驟WRT到@bpierre的答案是:
- 子類RegistrationView,即你的應用程序的views.py

class MyRegistrationView(RegistrationView): 
... 
def register(self, request, **cleaned_data): 
    ... 
    new_user = HtmlRegistrationProfile.objects.create_inactive_user(username, email, password, site) 

- 在你的urls.py更改視圖以子類的觀點,即 - 列表項

url(r'accounts/register/$', MyRegistrationView.as_view(form_class=RegistrationForm), name='registration_register'),'