Skip to content

Commit

Permalink
Allow named mailboxes in comm panel (#3752)
Browse files Browse the repository at this point in the history
* Allow named mailboxes in comm panel

* Revert director_email changes
  • Loading branch information
willgearty authored May 8, 2024
1 parent 7c9f9bc commit 1719f3b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion esp/esp/dbmail/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def send_mail(subject, message, from_email, recipient_list, fail_silently=False,
from_email = from_email.strip()
# the from_email must match one of our DMARC domains/subdomains
# or the email may be rejected by email clients
if not re.match(r"(^.+@%s>?$)|(^.+@(\w+\.)?learningu\.org>?$)" % settings.SITE_INFO[1].replace(".", "\."), from_email):
if not re.match(r'(^.+@{0}$)|(^.+<.+@{0}>$)|(^.+@(\w+\.)?learningu\.org$)|(^.+<.+@(\w+\.)?learningu\.org>$)'.format(settings.SITE_INFO[1].replace('.', '\.')), from_email):
raise ESPError("Invalid 'From' email address (" + from_email + "). The 'From' email address must " +
"end in @" + settings.SITE_INFO[1] + " (your website), " +
"@learningu.org, or a valid subdomain of learningu.org " +
Expand Down
4 changes: 2 additions & 2 deletions esp/esp/program/migrations/0027_auto_20240220_2124.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def replace_director_emails(apps, schema_editor):
Program = apps.get_model('program', 'Program')
for prog in Program.objects.all():
if not re.match(r"(^.+@%s$)|(^.+@(\w+\.)*learningu\.org$)" % settings.SITE_INFO[1].replace(".", "\."), prog.director_email):
if not re.match(r'(^.+@{0}$)|(^.+@(\w+\.)?learningu\.org$)'.format(settings.SITE_INFO[1].replace('.', '\.')), prog.director_email):
prog.director_email = 'info@' + settings.SITE_INFO[1]
prog.save()

Expand All @@ -35,7 +35,7 @@ class Migration(migrations.Migration):
help_text=b'The director email address must end in @' + settings.SITE_INFO[1] +
' (your website), @learningu.org, or a valid subdomain of learningu.org (i.e., @subdomain.learningu.org). The default is <b>info@' + settings.SITE_INFO[1] +
'</b>, which redirects to the "default" email address from your site\'s settings by default. You can create and manage your email redirects <a href="/manage/redirects/">here</a>.',
validators=[validators.RegexValidator(r'(^.+@%s$)|(^.+@(\w+\.)?learningu\.org$)' % settings.SITE_INFO[1].replace('.', '\.'))]),
validators=[validators.RegexValidator(r'(^.+@{0}$)|(^.+<.+@{0}>$)|(^.+@(\w+\.)?learningu\.org$)|(^.+<.+@(\w+\.)?learningu\.org>$)'.format(settings.SITE_INFO[1].replace('.', '\.')))]),
),
# This will run backwards, but won't do anything
migrations.RunPython(replace_director_emails, lambda a, s: None),
Expand Down
2 changes: 1 addition & 1 deletion esp/esp/program/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class Program(models.Model, CustomFormsLinkModel):
grade_max = models.IntegerField()
# director contact email address used for from field and display
director_email = models.EmailField(default='info@' + settings.SITE_INFO[1], max_length=75,
validators=[validators.RegexValidator(r'(^.+@%s$)|(^.+@(\w+\.)?learningu\.org$)' % settings.SITE_INFO[1].replace('.', '\.'))],
validators=[validators.RegexValidator(r'(^.+@{0}$)|(^.+@(\w+\.)?learningu\.org$)'.format(settings.SITE_INFO[1].replace('.', '\.')))],
help_text=mark_safe('The director email address must end in @' + settings.SITE_INFO[1] + ' (your website), ' +
'@learningu.org, or a valid subdomain of learningu.org (i.e., @subdomain.learningu.org). ' +
'The default is <b>info@' + settings.SITE_INFO[1] + '</b>, which redirects to the "default" ' +
Expand Down
2 changes: 1 addition & 1 deletion esp/esp/program/modules/forms/admincore.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Meta:
'program_modules': forms.SelectMultiple(attrs={'class': 'hidden-field'}),
}
model = Program
ProgramSettingsForm.base_fields['director_email'].widget = forms.EmailInput(attrs={'pattern': r'(^.+@%s$)|(^.+@(\w+\.)?learningu\.org$)' % settings.SITE_INFO[1].replace('.', '\.')})
ProgramSettingsForm.base_fields['director_email'].widget = forms.EmailInput(attrs={'pattern': r'(^.+@{0}$)|(^.+@(\w+\.)?learningu\.org$)'.format(settings.SITE_INFO[1].replace('.', '\.'))})

class TeacherRegSettingsForm(BetterModelForm):
""" Form for changing teacher class registration settings. """
Expand Down
11 changes: 7 additions & 4 deletions esp/esp/program/modules/handlers/commmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from esp.users.controllers.usersearch import UserSearchController
from esp.users.views.usersearch import get_user_checklist
from esp.dbmail.models import ActionHandler
from esp.tagdict.models import Tag
from django.template import Template
from django.template import Context as DjangoContext
from esp.middleware import ESPError
Expand Down Expand Up @@ -80,7 +81,7 @@ def commprev(self, request, tl, one, two, module, extra, prog):
# Set From address
if request.POST.get('from', '').strip():
fromemail = request.POST['from']
if not re.match(r"(^.+@%s$)|(^.+@(\w+\.)?learningu\.org$)" % settings.SITE_INFO[1].replace(".", "\."), fromemail):
if not re.match(r'(^.+@{0}$)|(^.+<.+@{0}>$)|(^.+@(\w+\.)?learningu\.org$)|(^.+<.+@(\w+\.)?learningu\.org>$)'.format(settings.SITE_INFO[1].replace('.', '\.')), fromemail):
raise ESPError("Invalid 'From' email address. The 'From' email address must " +
"end in @" + settings.SITE_INFO[1] + " (your website), " +
"@learningu.org, or a valid subdomain of learningu.org " +
Expand All @@ -90,7 +91,8 @@ def commprev(self, request, tl, one, two, module, extra, prog):
prs = PlainRedirect.objects.filter(original = "info")
if not prs.exists():
redirect = PlainRedirect.objects.create(original = "info", destination = settings.DEFAULT_EMAIL_ADDRESSES['default'])
fromemail = '%s@%s' % ("info", settings.SITE_INFO[1])
fromemail = '%s <%s@%s>' % (Tag.getTag('full_group_name') or '%s %s' % (settings.INSTITUTION_NAME, settings.ORGANIZATION_SHORT_NAME),
"info", settings.SITE_INFO[1])

# Set Reply-To address
if request.POST.get('replyto', '').strip():
Expand Down Expand Up @@ -253,7 +255,8 @@ def commpanel(self, request, tl, one, two, module, extra, prog):
if request.method == 'POST':
# Turn multi-valued QueryDict into standard dictionary
data = ListGenModule.processPost(request)

context['default_from'] = '%s <%s@%s>' % (Tag.getTag('full_group_name') or '%s %s' % (settings.INSTITUTION_NAME, settings.ORGANIZATION_SHORT_NAME),
"info", settings.SITE_INFO[1])
## Handle normal list selecting submissions
if ('base_list' in data and 'recipient_type' in data) or ('combo_base_list' in data):

Expand All @@ -274,7 +277,7 @@ def commpanel(self, request, tl, one, two, module, extra, prog):
prs = PlainRedirect.objects.filter(original = "info")
if not prs.exists():
redirect = PlainRedirect.objects.create(original = "info", destination = settings.DEFAULT_EMAIL_ADDRESSES['default'])
context['from'] = '%s@%s' % ("info", settings.SITE_INFO[1])
context['from'] = context['default_from']
return render_to_response(self.baseDir()+'step2.html', request, context)

## Prepare a message starting from an earlier request
Expand Down
2 changes: 1 addition & 1 deletion esp/esp/users/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2885,7 +2885,7 @@ def send_confirmation_email(self):
subject, message = self._confirmation_email_content()
send_mail(subject,
message,
'info@' + settings.SITE_INFO[1],
Tag.getTag('full_group_name') or '%s %s' % (settings.INSTITUTION_NAME, settings.ORGANIZATION_SHORT_NAME) + ' <info@' + settings.SITE_INFO[1] + '>',
[self.requesting_student.email, ])

def get_admin_url(self):
Expand Down
5 changes: 3 additions & 2 deletions esp/templates/program/modules/commmodule/step2.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ <h2>Step 2:</h2>
By default the "From" email address is <b>info@{{ current_site.domain }}</b>, which redirects to the "default" email address from your site's settings.
You can create and manage your email redirects <a href="/manage/redirects/">here</a>.
The "Reply-To" field can be any email address (by default it is the same as the "From" email address).
Both email address fields also support named "mailboxes", such as "{{ default_from }}".
</div>

<form action="/manage/{{program.getUrlBase}}/commprev" method="post" name="comm2">
Expand All @@ -51,9 +52,9 @@ <h2>Step 2:</h2>
<td>
<label for="from">
<strong>From:</strong>
<small>(If blank: info@{{ current_site.domain }})</small>
<small>(If blank: {{ default_from }}</small>)</small>
</label>
<input type="text" size="30" name="from" id="from" value="{{from}}" pattern="(^.+@{{ current_site.domain|regexsite }}$)|(^.+@(\w+\.)?learningu\.org$)"/>
<input type="text" size="30" name="from" id="from" value="{{from}}" pattern="(^.+@{{ current_site.domain|regexsite }}$)|(^.+<.+@{{ current_site.domain|regexsite }}>$)|(^.+@(\w+\.)?learningu\.org$)|(^.+<.+@(\w+\.)?learningu\.org>$)"/>
<br /><br />
<label for="replyto">
<strong>Reply-To:</strong>
Expand Down

0 comments on commit 1719f3b

Please sign in to comment.