2009-08-25 29 views
11

django登録を使用して、すべてが問題なく、メールは平文で送信されていましたが、固定されていて、htmlで送信していますが、 HTMLコードが表示されます。django + django-registrationでhtmlでメールを送信

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

と私はいけないが...

任意のアイデアのようなHTMLコードを表示する必要がありますか?

ありがとうございます。

答えて

14

テキストバージョンとHTMLバージョンの両方を送信することをおすすめします。以下のためのジャンゴ登録の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、しかし、私は今そのようにして何もしていた...しかし、動作していないが、今ちょうど<なしのリンクを入れて、[OK] :)です... – Asinox

+0

これは、一部のクライアントがリンクを作成するテキストメールを送信します。もっと面白いhtmlが必要な場合は、私が推奨したことをする必要があります。 –

+0

はい、私は試してみましたが、仕事はしませんでしたが、大丈夫です:)もっとうまくいってください:) – Asinox

27

からのような何かを、あなたが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() 

登録バックエンドでRegistrationProfileの代わりにHtmlRegistrationProfileを使用してください。

+0

これは行く方法です。よくやった。 – ajt

+0

登録バックエンドで新しいプロファイルを登録するにはどうすればよいですか? – Sam

+10

RegistrationProfileではなく、HtmlRegistrationプロファイルにバックエンドを設定するにはどうすればよいですか? – AlexBrand

2

私はこれが古いではないと登録パッケージは、もはや維持されている知っているバックエンドの一部を使用することができました。ちょうど誰かがまだこれを望んでいる場合に備えて。 @bpierreの答えに追加の手順をWRTは、次のとおりです。
- あなたのアプリのviews.py

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

すなわち、RegistrationViewをサブクラス化 - あなたのurls.pyにサブ分類ビューにビューを変更する、すなわち - リスト項目

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