models.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:django-registration 作者: plivo 项目源码 文件源码
def send_activation_email(self, site, name=""):
        """
        Send an activation email to the user associated with this
        ``RegistrationProfile``.

        The activation email will make use of two templates:

        ``registration/activation_email_subject.txt``
            This template will be used for the subject line of the
            email. Because it is used as the subject line of an email,
            this template's output **must** be only a single line of
            text; output longer than one line will be forcibly joined
            into only a single line.

        ``registration/activation_email.txt``
            This template will be used for the body of the email.

        These templates will each receive the following context
        variables:

        ``activation_key``
            The activation key for the new account.

        ``expiration_days``
            The number of days remaining during which the account may
            be activated.

        ``site``
            An object representing the site on which the user
            registered; depending on whether ``django.contrib.sites``
            is installed, this may be an instance of either
            ``django.contrib.sites.models.Site`` (if the sites
            application is installed) or
            ``django.contrib.sites.models.RequestSite`` (if
            not). Consult the documentation for the Django sites
            framework for details regarding these objects' interfaces.

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

        html_content = render_to_string('registration/activation_email.html',
                                        ctx_dict)

        text_content = render_to_string('registration/activation_email.txt',
                                        ctx_dict)  # so people will have the text as well.

        # create the email, and attach the HTML version as well.
        try:
            msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [self.user.email])
            msg.attach_alternative(html_content, "text/html")
            msg.send()
        except smtplib.SMTPException:
            self.user.email_user(subject, text_content, settings.DEFAULT_FROM_EMAIL)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号