diff --git a/cms/djangoapps/course_creators/admin.py b/cms/djangoapps/course_creators/admin.py index 345fb52325d5..2e7c90521b15 100644 --- a/cms/djangoapps/course_creators/admin.py +++ b/cms/djangoapps/course_creators/admin.py @@ -6,6 +6,7 @@ import logging from smtplib import SMTPException +from crum import get_current_request from django.conf import settings from django.contrib import admin from django.core.mail import send_mail @@ -14,6 +15,8 @@ from course_creators.models import CourseCreator, send_admin_notification, send_user_notification, update_creator_state from course_creators.views import update_course_creator_group from edxmako.shortcuts import render_to_string +from openedx.features.edly.constants import ROLE_ASSIGNED, ROLE_REVOKED +from openedx.features.edly.utils import is_config_enabled log = logging.getLogger("studio.coursecreatoradmin") @@ -104,10 +107,13 @@ def send_user_notification_callback(sender, **kwargs): else: # changed to unrequested or pending message_template = 'emails/course_creator_revoked.txt' - message = render_to_string(message_template, context) + message = render_to_string(message_template, context) + config_key = ROLE_ASSIGNED if updated_state == CourseCreator.GRANTED else ROLE_REVOKED try: - user.email_user(subject, message, studio_request_email) + site = get_current_request().site + if is_config_enabled(site, config_key): + user.email_user(subject, message, studio_request_email) except: log.warning(u"Unable to send course creator status e-mail to %s", user.email) diff --git a/lms/djangoapps/instructor/enrollment.py b/lms/djangoapps/instructor/enrollment.py index 0e865d9a4744..4258d8dba7d6 100644 --- a/lms/djangoapps/instructor/enrollment.py +++ b/lms/djangoapps/instructor/enrollment.py @@ -11,6 +11,7 @@ import pytz import six +from crum import get_current_request from django.conf import settings from django.contrib.auth.models import User from django.core.mail import send_mail @@ -21,6 +22,8 @@ from edx_ace.recipient import Recipient from eventtracking import tracker from six import text_type +from openedx.features.edly.constants import COURSE_ENROLLMENT +from openedx.features.edly.utils import is_config_enabled from submissions import api as sub_api # installed from the edx-submissions repository from submissions.models import score_set @@ -124,7 +127,7 @@ def get_user_email_language(user): def enroll_email( course_id, student_email, auto_enroll=False, email_students=False, - email_params=None, language=None, context_vars=None + email_params=None, language=None, context_vars=None, site=None ): """ Enroll a student by email. @@ -165,7 +168,7 @@ def enroll_email( email_params['message_type'] = 'enrolled_enroll' email_params['email_address'] = student_email email_params['full_name'] = previous_state.full_name - send_mail_to_student(student_email, email_params, language=language, context_vars=context_vars) + send_mail_to_student(student_email, email_params, language=language, context_vars=context_vars, site=site) elif not is_email_retired(student_email): cea, _ = CourseEnrollmentAllowed.objects.get_or_create(course_id=course_id, email=student_email) @@ -174,7 +177,7 @@ def enroll_email( if email_students: email_params['message_type'] = 'allowed_enroll' email_params['email_address'] = student_email - send_mail_to_student(student_email, email_params, language=language, context_vars=context_vars) + send_mail_to_student(student_email, email_params, language=language, context_vars=context_vars, site=site) after_state = EmailEnrollmentState(course_id, student_email) @@ -425,7 +428,7 @@ def get_email_params(course, auto_enroll, secure=True, course_key=None, display_ return email_params -def send_mail_to_student(student, param_dict, language=None, context_vars=None): +def send_mail_to_student(student, param_dict, language=None, context_vars=None, site=None): """ Construct the email using templates and then send it. `student` is the student's email address (a `str`), @@ -450,7 +453,7 @@ def send_mail_to_student(student, param_dict, language=None, context_vars=None): Returns a boolean indicating whether the email was sent successfully. """ - + site = site or get_current_request().site # Add some helpers and microconfig subsitutions if 'display_name' in param_dict: param_dict['course_name'] = param_dict['display_name'] @@ -490,7 +493,8 @@ def send_mail_to_student(student, param_dict, language=None, context_vars=None): if from_email: message.options.update({'from_address': from_email}) - ace.send(message) + if is_config_enabled(site, COURSE_ENROLLMENT): + ace.send(message) def render_message_to_string(subject_template, message_template, param_dict, language=None): diff --git a/lms/djangoapps/instructor_task/tasks_helper/enrollments.py b/lms/djangoapps/instructor_task/tasks_helper/enrollments.py index a3f7d494afdd..af87f0ef8f45 100644 --- a/lms/djangoapps/instructor_task/tasks_helper/enrollments.py +++ b/lms/djangoapps/instructor_task/tasks_helper/enrollments.py @@ -450,7 +450,7 @@ def enroll_user_to_course(request_info, course_id, username_or_email, site_name= email_params = get_email_params(course=course, auto_enroll=auto_enroll, site_name=site_name) __ = enroll_email( course_id, email, auto_enroll, email_students, - email_params, language=language, context_vars=context_vars, + email_params, language=language, context_vars=context_vars, site=thread_site ) if user: TASK_LOG.info( diff --git a/openedx/features/edly/constants.py b/openedx/features/edly/constants.py index ad6c92e609b7..9e983ee5b328 100644 --- a/openedx/features/edly/constants.py +++ b/openedx/features/edly/constants.py @@ -19,3 +19,10 @@ WP_ADMIN_USERS = 'wp_admin_users' COURSE_AUTHORS = 'course_authors' PANEL_ADMINS = 'panel_admins' + +# EMAIL CONFIGS +ACCOUNT_STATUS = 'account_status' +COURSE_ENROLLMENT = 'course_enrollment' +ROLE_ASSIGNED = 'role_assigned' +ROLE_REVOKED = 'role_revoked' +SUBSCRIPTION_EXPIRE = 'subscription_expire' diff --git a/openedx/features/edly/utils.py b/openedx/features/edly/utils.py index c57309528893..3b4e45b12ad0 100644 --- a/openedx/features/edly/utils.py +++ b/openedx/features/edly/utils.py @@ -590,3 +590,15 @@ def send_cert_email_to_course_staff(student_email, course_key, site_id, context_ msg.options['from_address'] = from_address send_certificate_generation_email(msg, instructor_user, site) + + +def is_config_enabled(site, conf_key): + """ + Returns if the given email is enabled or not. + """ + site_conf = getattr(site, "configuration", None) + if not site_conf: + return True + + email_conf = site_conf.get_value('EMAILS_CONFIG', {}) + return email_conf.get(conf_key, True) diff --git a/openedx/features/subscriptions/management/commands/send_subscriptions_expiry_emails.py b/openedx/features/subscriptions/management/commands/send_subscriptions_expiry_emails.py index db109b3d48eb..f6c283f793d7 100644 --- a/openedx/features/subscriptions/management/commands/send_subscriptions_expiry_emails.py +++ b/openedx/features/subscriptions/management/commands/send_subscriptions_expiry_emails.py @@ -15,7 +15,9 @@ from openedx.core.djangoapps.theming.helpers import get_config_value_from_site_or_settings from openedx.core.djangoapps.user_api.preferences.api import get_user_preference from openedx.core.lib.celery.task_utils import emulate_http_request +from openedx.features.edly.constants import SUBSCRIPTION_EXPIRE from openedx.features.edly.context_processor import Colour +from openedx.features.edly.utils import is_config_enabled from openedx.features.subscriptions.message_types import ExpiredNotification, ImpendingExpiryNotification from openedx.features.subscriptions.models import UserSubscription from openedx.features.subscriptions.utils import get_subscription_renew_url @@ -94,8 +96,9 @@ def _send_email_notifications(self, context_values, ace_message_class): language=get_user_preference(user, LANGUAGE_KEY), user_context={'full_name': user.profile.name} ) - ace.send(msg) - logger.info('Expiry notification email sent to user: %r', user.username) + if is_config_enabled(site, SUBSCRIPTION_EXPIRE): + ace.send(msg) + logger.info('Expiry notification email sent to user: %r', user.username) except Exception: # pylint: disable=broad-except logger.exception('Could not send email for subscription expiry notification to user %s', user.username)