Example #1
0
def send_comment_email(comment):
    site = get_current_site().domain
    subject = '感谢您发表的评论'
    article_url = "https://{site}{path}".format(
        site=site, path=comment.article.get_absolute_url())
    html_content = """
                   <p>非常感谢您在本站发表评论</p>
                   您可以访问
                   <a href="%s" rel="bookmark">%s</a>
                   来查看您的评论,
                   再次感谢您!
                   <br />
                   如果上面链接无法打开,请将此链接复制至浏览器。
                   %s
                   """ % (article_url, comment.article.title, article_url)
    tomail = comment.author.email
    send_email([tomail], subject, html_content)
    try:
        if comment.parent_comment:
            html_content = """
                    您在 <a href="%s" rel="bookmark">%s</a> 的评论 <br/> %s <br/> 收到回复啦.快去看看吧
                    <br/>
                    如果上面链接无法打开,请将此链接复制至浏览器。
                    %s
                    """ % (article_url, comment.article.title, comment.parent_comment.body, article_url)
            tomail = comment.parent_comment.author.email
            send_email([tomail], subject, html_content)
    except Exception as e:
        logger.error(e)
Example #2
0
    def form_valid(self, form):
        email = form.cleaned_data['email']
        oauthid = form.cleaned_data['oauthid']
        oauthuser = get_object_or_404(OAuthUser, pk=oauthid)
        oauthuser.email = email
        oauthuser.save()
        sign = get_sha256(settings.SECRET_KEY + str(oauthuser.id) +
                          settings.SECRET_KEY)
        site = get_current_site().domain
        if settings.DEBUG:
            site = '127.0.0.1:8000'
        path = reverse('oauth:email_confirm',
                       kwargs={
                           'id': oauthid,
                           'sign': sign
                       })
        url = "http://{site}{path}".format(site=site, path=path)

        content = """
                <p>请点击下面链接绑定您的邮箱</p>

                <a href="{url}" rel="bookmark">{url}</a>

                再次感谢您!
                <br />
                如果上面链接无法打开,请将此链接复制至浏览器。
                {url}
                """.format(url=url)
        send_email(emailto=[
            email,
        ], title='绑定您的电子邮箱', content=content)
        url = reverse('oauth:bindsuccess', kwargs={'oauthid': oauthid})
        url = url + '?type=email'
        return HttpResponseRedirect(url)
Example #3
0
def send_verify_email(to_mail: str, code: str, subject: str = "邮件验证码"):
    """发送重设密码验证码
    Args:
        to_mail: 接受邮箱
        subject: 邮件主题
        code: 验证码
    """
    html_content = f"您正在重设密码,验证码为:{code}, 5分钟内有效,请妥善保管"
    send_email([to_mail], subject, html_content)
Example #4
0
def emailconfirm(request, id, sign):
    if not sign:
        return HttpResponseForbidden()
    if not get_sha256(settings.SECRET_KEY + str(id) +
                      settings.SECRET_KEY).upper() == sign.upper():
        return HttpResponseForbidden()
    oauthuser = get_object_or_404(OAuthUser, pk=id)
    with transaction.atomic():
        if oauthuser.author:
            author = get_user_model().objects.get(pk=oauthuser.author_id)
        else:
            result = get_user_model().objects.get_or_create(
                email=oauthuser.email)
            author = result[0]
            if result[1]:
                author.source = 'emailconfirm'
                author.username = oauthuser.nikename.strip(
                ) if oauthuser.nikename.strip(
                ) else "djangoblog" + datetime.datetime.now().strftime(
                    '%y%m%d%I%M%S')
                author.save()
        oauthuser.author = author
        oauthuser.save()
    oauth_user_login_signal.send(sender=emailconfirm.__class__,
                                 id=oauthuser.id)
    login(request, author)

    site = get_current_site().domain
    content = '''
     <p>恭喜您,您已经成功绑定您的邮箱,您可以使用{type}来直接免密码登录本网站.欢迎您继续关注本站,地址是</p>

                <a href="{url}" rel="bookmark">{url}</a>

                再次感谢您!
                <br />
                如果上面链接无法打开,请将此链接复制至浏览器。
                {url}
    '''.format(type=oauthuser.type, url='http://' + site)

    send_email(emailto=[
        oauthuser.email,
    ], title='恭喜您绑定成功!', content=content)
    url = reverse('oauth:bindsuccess', kwargs={'oauthid': id})
    url = url + '?type=success'
    return HttpResponseRedirect(url)
Example #5
0
    def form_valid(self, form):
        if form.is_valid():
            user = form.save(False)
            user.is_active = False
            user.source = 'Register'
            user.save(True)
            site = get_current_site().domain
            sign = get_sha256(get_sha256(settings.SECRET_KEY + str(user.id)))

            if settings.DEBUG:
                site = '127.0.0.1:8000'
            path = reverse('account:result')
            url = "http://{site}{path}?type=validation&id={id}&sign={sign}".format(
                site=site, path=path, id=user.id, sign=sign)

            content = """
                            <p>请点击下面链接验证您的邮箱</p>

                            <a href="{url}" rel="bookmark">{url}</a>

                            再次感谢您!
                            <br />
                            如果上面链接无法打开,请将此链接复制至浏览器。
                            {url}
                            """.format(url=url)
            send_email(emailto=[
                user.email,
            ],
                       title='验证您的电子邮箱',
                       content=content)

            url = reverse('accounts:result') + \
                  '?type=register&id=' + str(user.id)
            return HttpResponseRedirect(url)
        else:
            return self.render_to_response({'form': form})
Example #6
0
 def test_image(self):
     import requests
     rsp = requests.get(
         'https://www.python.org/static/img/[email protected]')
     imagepath = os.path.join(settings.BASE_DIR, 'python.png')
     with open(imagepath, 'wb') as file:
         file.write(rsp.content)
     rsp = self.client.post('/upload')
     self.assertEqual(rsp.status_code, 403)
     sign = get_sha256(get_sha256(settings.SECRET_KEY))
     with open(imagepath, 'rb') as file:
         imgfile = SimpleUploadedFile('python.png',
                                      file.read(),
                                      content_type='image/jpg')
         form_data = {'python.png': imgfile}
         rsp = self.client.post('/upload?sign=' + sign,
                                form_data,
                                follow=True)
         self.assertEqual(rsp.status_code, 200)
     os.remove(imagepath)
     from djangoblog.utils import save_user_avatar, send_email
     send_email(['*****@*****.**'], 'testTitle', 'testContent')
     save_user_avatar(
         'https://www.python.org/static/img/[email protected]')