diff --git a/seahub/auth/forms.py b/seahub/auth/forms.py index 46191fd7f7..bb4d991596 100644 --- a/seahub/auth/forms.py +++ b/seahub/auth/forms.py @@ -1,5 +1,4 @@ from django.contrib.sites.models import Site -from django.template import Context, loader from django import forms from django.utils.translation import ugettext_lazy as _ from django.utils.http import int_to_base36 @@ -7,7 +6,7 @@ from django.utils.http import int_to_base36 from seahub.base.accounts import User from seahub.auth import authenticate from seahub.auth.tokens import default_token_generator -from seahub.utils import IS_EMAIL_CONFIGURED, get_service_url +from seahub.utils import IS_EMAIL_CONFIGURED, send_html_email from captcha.fields import CaptchaField @@ -86,7 +85,6 @@ class PasswordResetForm(forms.Form): """ Generates a one-use only link for resetting password and sends to the user """ - from django.core.mail import EmailMessage user = self.users_cache if not domain_override: @@ -95,24 +93,15 @@ class PasswordResetForm(forms.Form): else: site_name = domain_override - service_url = get_service_url() - t = loader.get_template(email_template_name) - c = { 'email': user.username, - 'site_name': site_name, 'uid': int_to_base36(user.id), 'user': user, 'token': token_generator.make_token(user), - 'service_url': service_url, - 'media_url': settings.MEDIA_URL, - 'logo_path': settings.LOGO_PATH, } - msg = EmailMessage(_("Reset Password on %s") % site_name, - t.render(Context(c)), None, [user.username]) - msg.content_subtype = "html" # Main content is now text/html - msg.send() + send_html_email(_("Reset Password on %s") % site_name, + email_template_name, c, None, [user.username]) class SetPasswordForm(forms.Form): """ diff --git a/seahub/group/views.py b/seahub/group/views.py index 12d0401635..4595214b2e 100644 --- a/seahub/group/views.py +++ b/seahub/group/views.py @@ -6,7 +6,6 @@ import simplejson as json import urllib2 from django.conf import settings -from django.core.mail import EmailMessage from django.core.paginator import EmptyPage, InvalidPage from django.core.urlresolvers import reverse from django.contrib import messages @@ -50,11 +49,11 @@ from seahub.wiki import get_group_wiki_repo, get_group_wiki_page, convert_wiki_l get_wiki_pages from seahub.wiki.models import WikiDoesNotExist, WikiPageMissing, GroupWiki from seahub.wiki.utils import clean_page_name, get_wiki_dirent -from seahub.settings import SITE_ROOT, SITE_NAME, MEDIA_URL, LOGO_PATH +from seahub.settings import SITE_ROOT, SITE_NAME from seahub.shortcuts import get_first_object_or_none from seahub.utils import render_error, render_permission_error, string2list, \ check_and_get_org_by_group, gen_file_get_url, get_file_type_and_ext, \ - calc_file_path_hash, is_valid_username, get_service_url, send_html_email + calc_file_path_hash, is_valid_username, send_html_email from seahub.utils.file_types import IMAGE from seahub.utils.paginator import Paginator from seahub.views import is_registered_user diff --git a/seahub/notifications/management/commands/send_notices.py b/seahub/notifications/management/commands/send_notices.py index f98bda3f60..ff50e399df 100644 --- a/seahub/notifications/management/commands/send_notices.py +++ b/seahub/notifications/management/commands/send_notices.py @@ -3,13 +3,12 @@ import datetime import logging import string -from django.core.mail import EmailMessage from django.core.management.base import BaseCommand, CommandError from django.core.urlresolvers import reverse from seahub.base.models import CommandsLastCheck from seahub.notifications.models import UserNotification -from seahub.utils import get_service_url +from seahub.utils import send_html_email import seahub.settings as settings from django.template import Context, loader @@ -19,9 +18,6 @@ logger = logging.getLogger(__name__) site_name = settings.SITE_NAME subjects = (u'New notice on %s' % site_name, u'New notices on %s' % site_name) -service_url = get_service_url() -media_url = settings.MEDIA_URL -logo_path = settings.LOGO_PATH class Command(BaseCommand): help = 'Send Email notifications to user if he/she has an unread notices every period of seconds .' @@ -63,21 +59,15 @@ class Command(BaseCommand): for to_user, count in email_ctx.items(): subject = subjects[1] if count > 1 else subjects[0] - t = loader.get_template('notifications/notice_email.html') c = { - 'site_name': site_name, - 'media_url': media_url, - 'logo_path': logo_path, - 'service_url': service_url, 'to_user': to_user, 'notice_count': count, } try: - msg = EmailMessage(subject, t.render(Context(c)), settings.DEFAULT_FROM_EMAIL, - [to_user]) - msg.content_subtype = "html" - msg.send() + send_html_email(subject, 'notifications/notice_email.html', c, + settings.DEFAULT_FROM_EMAIL, [to_user]) + logger.info('Successfully sent email to %s' % to_user) except Exception, e: diff --git a/seahub/notifications/templates/notifications/notice_email.html b/seahub/notifications/templates/notifications/notice_email.html index dd2af967a2..eae086e5e8 100644 --- a/seahub/notifications/templates/notifications/notice_email.html +++ b/seahub/notifications/templates/notifications/notice_email.html @@ -17,7 +17,7 @@ You've got {{num}} new notices on {{ site_name }}.

{% trans "Go check out at the following page:" %}
-{{ service_url }}{% url 'user_notification_list' %} +{{ url_base }}{% url 'user_notification_list' %}

{% endautoescape %} diff --git a/seahub/share/views.py b/seahub/share/views.py index 92a20e2def..2b3bbd3a12 100644 --- a/seahub/share/views.py +++ b/seahub/share/views.py @@ -2,7 +2,6 @@ import os import logging import simplejson as json -from django.core.mail import EmailMessage from django.core.urlresolvers import reverse from django.db import IntegrityError from django.http import HttpResponse, HttpResponseRedirect, Http404, \ @@ -37,7 +36,7 @@ from seahub.views import validate_owner, is_registered_user from seahub.utils import render_permission_error, string2list, render_error, \ gen_token, gen_shared_link, gen_shared_upload_link, gen_dir_share_link, \ gen_file_share_link, IS_EMAIL_CONFIGURED, check_filename_with_rename, \ - get_repo_last_modify, is_valid_username, get_service_url + get_repo_last_modify, is_valid_username, send_html_email import seahub.settings as settings try: @@ -793,29 +792,21 @@ def send_shared_link(request): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] - t = loader.get_template('shared_link_email.html') to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) - service_url = get_service_url() c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, - 'site_name': SITE_NAME, - 'service_url': service_url, - 'media_url': settings.MEDIA_URL, - 'logo_path': settings.LOGO_PATH, } try: - msg = EmailMessage(_(u'A file is shared to you on s%') % SITE_NAME, - t.render(Context(c)), None, [to_email]) - msg.content_subtype = "html" - msg.send() + send_html_email(_(u'A file is shared to you on %s') % SITE_NAME, + 'shared_link_email.html', c, None, [to_email]) except Exception, e: logger.error(str(e)) data = json.dumps({'error':_(u'Internal server error. Send failed.')}) @@ -1072,29 +1063,21 @@ def send_shared_upload_link(request): email = form.cleaned_data['email'] shared_upload_link = form.cleaned_data['shared_upload_link'] - t = loader.get_template('shared_upload_link_email.html') to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) - service_url = get_service_url() c = { 'email': request.user.username, 'to_email': to_email, 'shared_upload_link': shared_upload_link, - 'site_name': SITE_NAME, - 'service_url': service_url, - 'media_url': settings.MEDIA_URL, - 'logo_path': settings.LOGO_PATH, } try: - msg = EmailMessage(_(u'An upload link is shared to you on s%') % SITE_NAME, - t.render(Context(c)), None, [to_email]) - msg.content_subtype = "html" - msg.send() + send_html_email(_(u'An upload link is shared to you on %s') % SITE_NAME, + 'shared_upload_link_email.html', c, None, [to_email]) except Exception, e: logger.error(str(e)) data = json.dumps({'error':_(u'Internal server error. Send failed.')}) diff --git a/seahub/templates/registration/password_reset_confirm.html b/seahub/templates/registration/password_reset_confirm.html index 60599500f6..983df86761 100644 --- a/seahub/templates/registration/password_reset_confirm.html +++ b/seahub/templates/registration/password_reset_confirm.html @@ -8,7 +8,7 @@ {% if validlink %}
-

{% trans "Input new password" %}

+

{% trans "Reset Password" %}

{% csrf_token %} {{ form.new_password1 }} diff --git a/seahub/templates/registration/password_reset_email.html b/seahub/templates/registration/password_reset_email.html index 4663eeacef..1a57de48c7 100644 --- a/seahub/templates/registration/password_reset_email.html +++ b/seahub/templates/registration/password_reset_email.html @@ -10,7 +10,7 @@

{% blocktrans with account=user.username %}To reset the password of your account {{ account }} on {{ site_name }}, please click the following link: {% endblocktrans %}
-{{ service_url }}{% url 'auth_password_reset_confirm' uidb36=uid token=token %} +{{ url_base }}{% url 'auth_password_reset_confirm' uidb36=uid token=token %}

diff --git a/seahub/templates/sysadmin/user_activation_email.html b/seahub/templates/sysadmin/user_activation_email.html index a6ce48ad09..1072de4f45 100644 --- a/seahub/templates/sysadmin/user_activation_email.html +++ b/seahub/templates/sysadmin/user_activation_email.html @@ -12,7 +12,7 @@ {% blocktrans %}Your account {{ username }} on {{ site_name }} has been activated.{% endblocktrans %}

-{% trans "Log In" %} +{% trans "Log In" %} {% endautoescape %} diff --git a/seahub/templates/sysadmin/user_add_email.html b/seahub/templates/sysadmin/user_add_email.html index 3398f4e70f..7b6690c51c 100644 --- a/seahub/templates/sysadmin/user_add_email.html +++ b/seahub/templates/sysadmin/user_add_email.html @@ -22,7 +22,7 @@ {% blocktrans %}Password: {{ password }}{% endblocktrans %}

-{% trans "Log In" %} +{% trans "Log In" %} {% endautoescape %} diff --git a/seahub/views/sysadmin.py b/seahub/views/sysadmin.py index aa2db89907..24d7f6b324 100644 --- a/seahub/views/sysadmin.py +++ b/seahub/views/sysadmin.py @@ -6,7 +6,6 @@ import logging import simplejson as json from django.core.urlresolvers import reverse -from django.core.mail import EmailMessage from django.contrib import messages from django.http import HttpResponse, Http404, HttpResponseRedirect from django.shortcuts import render_to_response @@ -31,9 +30,9 @@ from seahub.profile.models import Profile, DetailedProfile from seahub.share.models import FileShare import seahub.settings as settings -from seahub.settings import INIT_PASSWD, \ +from seahub.settings import INIT_PASSWD, SITE_NAME, \ SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER, SEND_EMAIL_ON_RESETTING_USER_PASSWD -from seahub.utils import get_site_scheme_and_netloc, get_service_url +from seahub.utils import send_html_email logger = logging.getLogger(__name__) @@ -407,20 +406,11 @@ def user_deactivate(request, user_id): def email_user_on_activation(user): """Send an email to user when admin activate his/her account. """ - service_url = get_service_url() - site_name = settings.SITE_NAME - t = loader.get_template('sysadmin/user_activation_email.html') c = { - 'site_name': site_name, - 'media_url': settings.MEDIA_URL, - 'logo_path': settings.LOGO_PATH, - 'service_url': service_url, 'username': user.email, } - msg = EmailMessage(_(u'Your account on %s is activated') % site_name, t.render(Context(c)), - None, [user.email]) - msg.content_subtype = "html" - msg.send() + send_html_email(_(u'Your account on %s is activated') % SITE_NAME, + 'sysadmin/user_activation_email.html', c, None, [user.email]) @login_required @sys_staff_required @@ -458,23 +448,13 @@ def send_user_reset_email(request, email, password): """ Send email when reset user password. """ - - service_url = get_service_url() - site_name = settings.SITE_NAME - t = loader.get_template('sysadmin/user_reset_email.html') c = { 'email': email, 'password': password, - 'site_name': site_name, - 'media_url': settings.MEDIA_URL, - 'logo_path': settings.LOGO_PATH, - 'service_url': service_url, } - msg = EmailMessage(_(u'Password has been reset on %s') % site_name, t.render(Context(c)), - None, [email]) - msg.content_subtype = "html" - msg.send() + send_html_email(_(u'Password has been reset on %s') % SITE_NAME, + 'sysadmin/user_reset_email.html', c, None, [email]) @login_required @sys_staff_required @@ -515,26 +495,14 @@ def user_reset(request, user_id): def send_user_add_mail(request, email, password): """Send email when add new user.""" - - service_url = get_service_url() - site_name = settings.SITE_NAME - - t = loader.get_template('sysadmin/user_add_email.html') c = { 'user': request.user.username, 'org': request.user.org, 'email': email, 'password': password, - 'service_url': service_url, - 'site_name': site_name, - 'media_url': settings.MEDIA_URL, - 'logo_path': settings.LOGO_PATH, } - msg = EmailMessage(_(u'You are invited to join %s') % site_name, t.render(Context(c)), - None, [email]) - msg.content_subtype = "html" - msg.send() - + send_html_email(_(u'You are invited to join %s') % SITE_NAME, + 'sysadmin/user_add_email.html', c, None, [email]) @login_required def user_add(request):