def send_auto_applied_license_email_task(enterprise_customer_uuid, user_email): """ Asynchronously sends onboarding email to learner. Intended for use following automatic license activation. Uses Braze client to send email via Braze campaign. """ try: # Get some info about the enterprise customer enterprise_api_client = EnterpriseApiClient() enterprise_customer = enterprise_api_client.get_enterprise_customer_data( enterprise_customer_uuid) enterprise_slug = enterprise_customer.get('slug') enterprise_name = enterprise_customer.get('name') learner_portal_search_enabled = enterprise_customer.get( 'enable_integrated_customer_learner_portal_search') identity_provider = enterprise_customer.get('identity_provider') enterprise_sender_alias = get_enterprise_sender_alias( enterprise_customer) enterprise_contact_email = enterprise_customer.get('contact_email') except Exception: # pylint: disable=broad-except message = ( f'Error getting data about the enterprise_customer {enterprise_customer_uuid}. ' f'Onboarding email to {user_email} for auto applied license failed.' ) logger.error(message, exc_info=True) return # Determine which email campaign to use if identity_provider and learner_portal_search_enabled is False: braze_campaign_id = settings.AUTOAPPLY_NO_LEARNER_PORTAL_CAMPAIGN else: braze_campaign_id = settings.AUTOAPPLY_WITH_LEARNER_PORTAL_CAMPAIGN # Form data we want to hand to the campaign's email template braze_trigger_properties = { 'enterprise_customer_slug': enterprise_slug, 'enterprise_customer_name': enterprise_name, 'enterprise_sender_alias': enterprise_sender_alias, 'enterprise_contact_email': enterprise_contact_email, } recipient = _aliased_recipient_object_from_email(user_email) try: # Hit the Braze api to send the email braze_client_instance = BrazeApiClient() braze_client_instance.create_braze_alias( [user_email], ENTERPRISE_BRAZE_ALIAS_LABEL, ) braze_client_instance.send_campaign_message( braze_campaign_id, recipients=[recipient], trigger_properties=braze_trigger_properties, ) except BrazeClientError: message = ( 'Error hitting Braze API. ' f'Onboarding email to {user_email} for auto applied license failed.' ) logger.error(message, exc_info=True)
def activation_email_task(custom_template_text, email_recipient_list, subscription_uuid): """ Sends license activation email(s) asynchronously, and creates pending enterprise users to link the email recipients to the subscription's enterprise. Arguments: custom_template_text (dict): Dictionary containing `greeting` and `closing` keys to be used for customizing the email template. email_recipient_list (list of str): List of recipients to send the emails to. subscription_uuid (str): UUID (string representation) of the subscription that the recipients are associated with or will be associated with. """ subscription_plan = SubscriptionPlan.objects.get(uuid=subscription_uuid) pending_licenses = subscription_plan.licenses.filter(user_email__in=email_recipient_list).order_by('uuid') enterprise_api_client = EnterpriseApiClient() enterprise_customer = enterprise_api_client.get_enterprise_customer_data(subscription_plan.enterprise_customer_uuid) enterprise_slug = enterprise_customer.get('slug') enterprise_name = enterprise_customer.get('name') enterprise_sender_alias = enterprise_customer.get('sender_alias', 'edX Support Team') try: send_activation_emails( custom_template_text, pending_licenses, enterprise_slug, enterprise_name, enterprise_sender_alias ) except SMTPException: msg = 'License manager activation email sending received an exception for enterprise: {}.'.format( enterprise_name ) logger.error(msg, exc_info=True) return
def _send_bulk_enrollment_results_email( bulk_enrollment_job, campaign_id, ): """ Sends email with properties required to detail the results of a bulk enrollment job. Arguments: bulk_enrollment_job (BulkEnrollmentJob): the completed bulk enrollment job campaign_id: (str): The Braze campaign identifier """ try: enterprise_api_client = EnterpriseApiClient() enterprise_customer = enterprise_api_client.get_enterprise_customer_data( bulk_enrollment_job.enterprise_customer_uuid, ) admin_users = enterprise_api_client.get_enterprise_admin_users( bulk_enrollment_job.enterprise_customer_uuid, ) # https://web.archive.org/web/20211122135949/https://www.braze.com/docs/api/objects_filters/recipient_object/ recipients = [] for user in admin_users: if int(user['id']) != bulk_enrollment_job.lms_user_id: continue # must use a mix of send_to_existing_only: false + enternal_id w/ attributes to send to new braze profiles recipient = { 'send_to_existing_only': False, 'external_user_id': str(user['id']), 'attributes': { 'email': user['email'], } } recipients.append(recipient) break braze_client = BrazeApiClient() braze_client.send_campaign_message(campaign_id, recipients=recipients, trigger_properties={ 'enterprise_customer_slug': enterprise_customer.get('slug'), 'enterprise_customer_name': enterprise_customer.get('name'), 'bulk_enrollment_job_uuid': str(bulk_enrollment_job.uuid), }) msg = ( f'success _send_bulk_enrollment_results_email for bulk_enrollment_job_uuid={bulk_enrollment_job.uuid} ' 'braze_campaign_id={campaign_id} lms_user_id={bulk_enrollment_job.lms_user_id}' ) logger.info(msg) except Exception as ex: msg = ( f'failed _send_bulk_enrollment_results_email for bulk_enrollment_job_uuid={bulk_enrollment_job.uuid} ' 'braze_campaign_id={campaign_id} lms_user_id={bulk_enrollment_job.lms_user_id}' ) logger.error(msg, exc_info=True) raise ex
def send_reminder_email_task(custom_template_text, email_recipient_list, subscription_uuid): """ Sends license activation reminder email(s) asynchronously. Arguments: custom_template_text (dict): Dictionary containing `greeting` and `closing` keys to be used for customizing the email template. email_recipient_list (list of str): List of recipients to send the emails to. subscription_uuid (str): UUID (string representation) of the subscription that the recipients are associated with or will be associated with. """ subscription_plan = SubscriptionPlan.objects.get(uuid=subscription_uuid) pending_licenses = subscription_plan.licenses.filter( user_email__in=email_recipient_list).order_by('uuid') enterprise_api_client = EnterpriseApiClient() enterprise_customer = enterprise_api_client.get_enterprise_customer_data( subscription_plan.enterprise_customer_uuid) enterprise_slug = enterprise_customer.get('slug') enterprise_name = enterprise_customer.get('name') enterprise_sender_alias = get_enterprise_sender_alias(enterprise_customer) enterprise_contact_email = enterprise_customer.get('contact_email') # We need to send these emails individually, because each email's text must be # generated for every single user/activation_key for pending_license in pending_licenses: user_email = pending_license.user_email license_activation_key = str(pending_license.activation_key) braze_campaign_id = settings.BRAZE_REMIND_EMAIL_CAMPAIGN braze_trigger_properties = { 'TEMPLATE_GREETING': custom_template_text['greeting'], 'TEMPLATE_CLOSING': custom_template_text['closing'], 'license_activation_key': license_activation_key, 'enterprise_customer_slug': enterprise_slug, 'enterprise_customer_name': enterprise_name, 'enterprise_sender_alias': enterprise_sender_alias, 'enterprise_contact_email': enterprise_contact_email, } recipient = _aliased_recipient_object_from_email(user_email) try: braze_client_instance = BrazeApiClient() braze_client_instance.create_braze_alias( [user_email], ENTERPRISE_BRAZE_ALIAS_LABEL, ) braze_client_instance.send_campaign_message( braze_campaign_id, recipients=[recipient], trigger_properties=braze_trigger_properties, ) except BrazeClientError as exc: message = ('Error hitting Braze API. ' f'reminder email to {user_email} for license failed.') logger.exception(message) raise exc License.set_date_fields_to_now(pending_licenses, ['last_remind_date'])
def send_revocation_cap_notification_email_task(subscription_uuid): """ Sends revocation cap email notification to ECS asynchronously. Arguments: subscription_uuid (str): UUID (string representation) of the subscription that has reached its recovation cap. """ subscription_plan = SubscriptionPlan.objects.get(uuid=subscription_uuid) enterprise_api_client = EnterpriseApiClient() enterprise_customer = enterprise_api_client.get_enterprise_customer_data( subscription_plan.enterprise_customer_uuid) enterprise_name = enterprise_customer.get('name') now = localized_utcnow() revocation_date = datetime.strftime(now, "%B %d, %Y, %I:%M%p %Z") braze_campaign_id = settings.BRAZE_REVOKE_CAP_EMAIL_CAMPAIGN braze_trigger_properties = { 'SUBSCRIPTION_TITLE': subscription_plan.title, 'NUM_REVOCATIONS_APPLIED': subscription_plan.num_revocations_applied, 'ENTERPRISE_NAME': enterprise_name, 'REVOKED_LIMIT_REACHED_DATE': revocation_date, } recipient = _aliased_recipient_object_from_email( settings.CUSTOMER_SUCCESS_EMAIL_ADDRESS) try: braze_client_instance = BrazeApiClient() braze_client_instance.create_braze_alias( [settings.CUSTOMER_SUCCESS_EMAIL_ADDRESS], ENTERPRISE_BRAZE_ALIAS_LABEL, ) braze_client_instance.send_campaign_message( braze_campaign_id, recipients=[recipient], trigger_properties=braze_trigger_properties, ) except BrazeClientError as exc: message = 'Revocation cap notification email sending received an exception.' logger.exception(message) raise exc
def send_revocation_cap_notification_email_task(subscription_uuid): """ Sends revocation cap email notification to ECS asynchronously. Arguments: subscription_uuid (str): UUID (string representation) of the subscription that has reached its recovation cap. """ subscription_plan = SubscriptionPlan.objects.get(uuid=subscription_uuid) enterprise_api_client = EnterpriseApiClient() enterprise_customer = enterprise_api_client.get_enterprise_customer_data(subscription_plan.enterprise_customer_uuid) enterprise_name = enterprise_customer.get('name') enterprise_sender_alias = enterprise_customer.get('sender_alias', 'edX Support Team') try: send_revocation_cap_notification_email( subscription_plan, enterprise_name, enterprise_sender_alias, ) except SMTPException: logger.error('Revocation cap notification email sending received an exception.', exc_info=True)
def send_reminder_email_task(custom_template_text, email_recipient_list, subscription_uuid): """ Sends license activation reminder email(s) asynchronously. Arguments: custom_template_text (dict): Dictionary containing `greeting` and `closing` keys to be used for customizing the email template. email_recipient_list (list of str): List of recipients to send the emails to. subscription_uuid (str): UUID (string representation) of the subscription that the recipients are associated with or will be associated with. """ subscription_plan = SubscriptionPlan.objects.get(uuid=subscription_uuid) pending_licenses = subscription_plan.licenses.filter(user_email__in=email_recipient_list).order_by('uuid') enterprise_api_client = EnterpriseApiClient() enterprise_customer = enterprise_api_client.get_enterprise_customer_data(subscription_plan.enterprise_customer_uuid) enterprise_slug = enterprise_customer.get('slug') enterprise_name = enterprise_customer.get('name') enterprise_sender_alias = get_enterprise_sender_alias(enterprise_customer) try: send_activation_emails( custom_template_text, pending_licenses, enterprise_slug, enterprise_name, enterprise_sender_alias, is_reminder=True ) except SMTPException: msg = 'License manager reminder email sending received an exception for enterprise: {}.'.format( enterprise_name ) logger.error(msg, exc_info=True) # Return without updating the last_remind_date for licenses return License.set_date_fields_to_now(pending_licenses, ['last_remind_date'])