22Celery task for course enrollment email
33"""
44import logging
5+ from datetime import datetime
6+
57from celery import shared_task
68from django .conf import settings
79from django .contrib .auth import get_user_model
2931MAX_RETRIES = 3
3032
3133
34+ def _build_enrollment_email_image_urls (language = 'en' ):
35+ """
36+ Build absolute URLs for enrollment email images (SES only).
37+
38+ This function constructs full image URLs for SES (Simple Email Service) email delivery. These URLs are required because SES email clients cannot resolve relative paths or Django static file paths—they need absolute URLs. Braze does NOT use these image URL keys; they are ignored by Braze and only used when the SES path is enabled.
39+
40+ Args:
41+ language (str): Language code ('en' or 'es') to select correct image variants
42+
43+ Returns:
44+ dict: Mapping of image variable names to absolute URLs
45+
46+ How it works:
47+ 1. settings.LMS_ROOT_URL is evaluated at runtime (e.g., 'https://courses.edx.org')
48+ 2. f-string interpolates this value into the path
49+ 3. SES email client receives complete URL: 'https://courses.edx.org/static/images/enrollment_email/...'
50+ 4. Browser/email client can fetch image directly without context about Django
51+
52+ Example flow:
53+ - Python (settings): LMS_ROOT_URL = 'https://courses.edx.org'
54+ - f-string: f"{settings.LMS_ROOT_URL}/static/images/enrollment_email/person_icon_{language}.png"
55+ - Result: 'https://courses.edx.org/static/images/enrollment_email/person_icon_en.png'
56+ - SES email client: Makes HTTP GET to that URL, renders image
57+ """
58+ lms_root = configuration_helpers .get_value (
59+ "LMS_ROOT_URL" , settings .LMS_ROOT_URL
60+ ).rstrip ('/' )
61+
62+ # NOTE: Image URLs for SES templates.
63+ # Currently not used in Braze flow — will be enabled during SES migration.
64+ # Construct image URLs based on language
65+ image_urls = {
66+ 'logo_url' : f"{ lms_root } /static/images/edx_logo.png" ,
67+ 'you_are_enrolled_en' : f"{ lms_root } /static/images/enrollment_email/you_are_enrolled_en.png" ,
68+ 'banner_default' : f"{ lms_root } /static/images/enrollment_email/banner_default.png" ,
69+ 'timer_icon_en' : f"{ lms_root } /static/images/enrollment_email/timer_icon_en.png" ,
70+ 'person_icon_en' : f"{ lms_root } /static/images/enrollment_email/person_icon_en.png" ,
71+ 'dollar_icon_en' : f"{ lms_root } /static/images/enrollment_email/dollar_icon_en.png" ,
72+ 'goal_idea_icon_en' : f"{ lms_root } /static/images/enrollment_email/goal_idea_icon_en.png" ,
73+ 'flag_icon_pink_en' : f"{ lms_root } /static/images/enrollment_email/flag_icon_pink_en.png" ,
74+ 'flag_icon_black_en_es' : f"{ lms_root } /static/images/enrollment_email/flag_icon_black_en_es.png" ,
75+ 'flag_icon_orange_en' : f"{ lms_root } /static/images/enrollment_email/flag_icon_orange_en.png" ,
76+ 'vertical_line_white_en' : f"{ lms_root } /static/images/enrollment_email/vertical_line_white_en.png" ,
77+ 'vertical_line_orange_en' : f"{ lms_root } /static/images/enrollment_email/vertical_line_orange_en.png" ,
78+ 'vertical_line_black_en' : f"{ lms_root } /static/images/enrollment_email/vertical_line_black_en.png" ,
79+ 'community_illustration_en' : f"{ lms_root } /static/images/enrollment_email/community_illustration_en.png" ,
80+ }
81+
82+ # If Spanish, override with Spanish variants
83+ if language == 'es' :
84+ spanish_overrides = {
85+ 'banner_default' : f"{ lms_root } /static/images/enrollment_email/banner_default.png" , # Same for both languages
86+ 'arrow_icon_es' : f"{ lms_root } /static/images/enrollment_email/arrow_icon_es.png" ,
87+ 'timer_icon_es' : f"{ lms_root } /static/images/enrollment_email/timer_icon_es.png" ,
88+ 'person_icon_es' : f"{ lms_root } /static/images/enrollment_email/person_icon_es.png" ,
89+ 'dollar_icon_es' : f"{ lms_root } /static/images/enrollment_email/dollar_icon_es.png" ,
90+ 'flag_icon_white_es' : f"{ lms_root } /static/images/enrollment_email/flag_icon_white_es.png" ,
91+ 'flag_icon_grey_es' : f"{ lms_root } /static/images/enrollment_email/flag_icon_grey_es.png" ,
92+ 'flag_icon_black_en_es' : f"{ lms_root } /static/images/enrollment_email/flag_icon_black_en_es.png" , # Same for both
93+ 'calendar_icon_es' : f"{ lms_root } /static/images/enrollment_email/calendar_icon_es.png" ,
94+ 'community_icon_es' : f"{ lms_root } /static/images/enrollment_email/community_icon_es.png" ,
95+ 'slant_line_es' : f"{ lms_root } /static/images/enrollment_email/slant_line_es.png" ,
96+ }
97+ image_urls .update (spanish_overrides )
98+
99+ return image_urls
100+
101+
32102@shared_task (bind = True , ignore_result = True )
33103@set_code_owner_attribute
34104def send_course_enrollment_email (
@@ -64,13 +134,16 @@ def send_course_enrollment_email(
64134 "course_price" : CourseMode .min_course_price_for_currency (
65135 course_id = course_id , currency = "USD"
66136 ),
137+ # Strip trailing slashes so template concatenation like
138+ # "{{ lms_base_url }}/courses/..." never produces a double slash.
67139 "lms_base_url" : configuration_helpers .get_value (
68140 "LMS_ROOT_URL" , settings .LMS_ROOT_URL
69- ),
141+ ). rstrip ( '/' ) ,
70142 "learning_base_url" : configuration_helpers .get_value (
71143 "LEARNING_MICROFRONTEND_URL" , settings .LEARNING_MICROFRONTEND_URL
72- ),
73- "track_mode" : track_mode
144+ ).rstrip ('/' ),
145+ "track_mode" : track_mode ,
146+ "current_year" : datetime .now ().year ,
74147 }
75148
76149 try :
@@ -86,9 +159,14 @@ def send_course_enrollment_email(
86159 {
87160 "course_date_blocks" : course_date_blocks ,
88161 "goals_enabled" : ENABLE_COURSE_GOALS .is_enabled (course_key ),
162+ "user_name" : user .get_full_name () or user .first_name or user .username ,
89163 }
90164 )
91165
166+ # Inject absolute image URLs for SES rendering. Braze ignores these extra
167+ # keys; they're consumed only when the SES path is enabled.
168+ canvas_entry_properties .update (_build_enrollment_email_image_urls (language = 'en' ))
169+
92170 try :
93171 course_uuid = get_course_uuid_for_course (course_id )
94172 if course_uuid is None :
0 commit comments