-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ŝanĝas la retpoŝtan provizanton al Postmark per Anymail
adiaŭ SendGrid!
- Loading branch information
Showing
25 changed files
with
341 additions
and
66 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from django.apps import AppConfig | ||
from django.conf import settings | ||
from django.dispatch import receiver | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from anymail.backends.base import AnymailBaseBackend | ||
from anymail.message import AnymailMessage | ||
from anymail.signals import pre_send | ||
|
||
|
||
class ChatConfig(AppConfig): | ||
name = "chat" | ||
verbose_name = _("Communicator") | ||
|
||
|
||
@receiver(pre_send) | ||
def enrich_envelope(sender: type[AnymailBaseBackend], message: AnymailMessage, **kwargs): | ||
if getattr(message, 'mass_mail', False): | ||
# Mass emails must be sent via the broadcast stream. | ||
return | ||
if message.subject.startswith('[[CHAT]]'): | ||
extra = getattr(message, 'esp_extra', {}) | ||
extra['MessageStream'] = 'notifications-to-users' | ||
message.esp_extra = extra | ||
message.tags = ['notification:chat'] | ||
message.track_opens = True | ||
message.metadata = {'env': settings.ENVIRONMENT} | ||
message.subject = message.subject.removeprefix('[[CHAT]]').strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{% load i18n %}{% autoescape off %} | ||
|
||
{% if RICH_ENVELOPE %}[[ACCOUNT-EMAIL]]{% endif %} | ||
|
||
{{ subject_prefix }}{% trans "Change of email address" context "Email subject" %} | ||
|
||
{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{% load i18n %}{% autoescape off %} | ||
|
||
{% if RICH_ENVELOPE %}[[ACCOUNT-EMAIL]]{% endif %} | ||
|
||
{{ subject_prefix }}{% trans "Change of email address" context "Email subject" %} | ||
|
||
{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{% load i18n %}{% autoescape off %} | ||
|
||
{% if RICH_ENVELOPE %}[[ACCOUNT]]{% endif %} | ||
|
||
{{ subject_prefix }}{% trans "Password reset" context "Email subject" %} | ||
|
||
{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<div class="row well base-form"> | ||
<h5 class="col-xs-3"> | ||
<strong data-toggle="tooltip" title="[email protected]">Pasporta Servo</strong> | ||
<strong data-toggle="tooltip" title="{{ mailing_address|default:"[email protected]" }}">Pasporta Servo</strong> | ||
</h5> | ||
<h5 class="col-xs-9 text-left"> | ||
<span id="preview_subject"></span> | ||
<small>- <span id="preview_preheader"></span></small> | ||
<small>– <span id="preview_preheader"></span></small> | ||
</h5> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{% load i18n %}{% autoescape off %} | ||
|
||
{% if RICH_ENVELOPE %}[[ACCOUNT-EMAIL]]{% endif %} | ||
|
||
{{ subject_prefix }}{% trans "Is this your email address?" context "Email subject" %} | ||
|
||
{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{% load i18n %}{% autoescape off %} | ||
|
||
{% if RICH_ENVELOPE %}[[ACCOUNT]]{% endif %} | ||
|
||
{{ subject_prefix }}{% trans "Username reminder" context "Email subject" %} | ||
|
||
{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,19 @@ | |
import operator | ||
import re | ||
from functools import reduce | ||
from typing import Optional, Sequence, Tuple | ||
|
||
from django.conf import settings | ||
from django.core.mail import EmailMultiAlternatives, get_connection | ||
from django.core.mail import EmailMessage, get_connection | ||
from django.core.mail.backends.base import BaseEmailBackend | ||
from django.utils.functional import ( | ||
SimpleLazyObject, keep_lazy_text, lazy, new_method_proxy, | ||
) | ||
from django.utils.http import url_has_allowed_host_and_scheme | ||
from django.utils.safestring import mark_safe | ||
|
||
import requests | ||
from anymail.message import AnymailMessage | ||
|
||
|
||
def getattr_(obj, path): | ||
|
@@ -49,8 +52,12 @@ def _lazy_joiner(sep, items, item_to_string=str): | |
setattr(SimpleLazyObject, '__mul__', new_method_proxy(operator.mul)) | ||
|
||
|
||
def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None, | ||
connection=None): | ||
def send_mass_html_mail( | ||
datatuple: Sequence[Tuple[str, str, str, Optional[str], Sequence[str] | None]], | ||
fail_silently: bool = False, | ||
auth_user: Optional[str] = None, auth_password: Optional[str] = None, | ||
connection: Optional[BaseEmailBackend] = None, | ||
) -> int: | ||
""" | ||
Given a datatuple of (subject, text_content, html_content, from_email, | ||
recipient_list), sends each message to each recipient list. Returns the | ||
|
@@ -62,16 +69,23 @@ def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None | |
If auth_password is None, the EMAIL_HOST_PASSWORD setting is used. | ||
""" | ||
connection = connection or get_connection( | ||
username=user, password=password, fail_silently=fail_silently) | ||
messages = [] | ||
username=auth_user, password=auth_password, fail_silently=fail_silently) | ||
messages: Sequence[EmailMessage] = [] | ||
default_from = settings.DEFAULT_FROM_EMAIL | ||
for subject, text, html, from_email, recipients in datatuple: | ||
subject = ''.join(subject.splitlines()) | ||
recipients = [r.strip() for r in recipients] | ||
message = EmailMultiAlternatives( | ||
subject, text, default_from, recipients, | ||
recipients = [r.strip() for r in recipients] if recipients else [] | ||
message = AnymailMessage( | ||
subject, text, from_email or default_from, recipients, | ||
headers={'Reply-To': 'Pasporta Servo <[email protected]>'}) | ||
message.attach_alternative(html, 'text/html') | ||
# TODO: Implement custom one-click unsubscribe. | ||
message.esp_extra = {'MessageStream': 'broadcast'} | ||
if tag_match := re.match(r'\[\[([a-zA-Z0-9_-]+)\]\]', subject): | ||
message.tags = [tag_match.group(1)] | ||
message.subject = subject.removeprefix(tag_match.group()).strip() | ||
message.merge_data = {} # Enable batch sending mode. | ||
setattr(message, 'mass_mail', True) | ||
messages.append(message) | ||
return connection.send_messages(messages) or 0 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -349,7 +349,7 @@ class PasswordResetView(PasswordResetBuiltinView): | |
""" | ||
This extension of Django's built-in view allows to send a different | ||
email depending on whether the user is active (True) or no (False). | ||
See also the companion SystemPasswordResetRequestForm. | ||
See also the companion `SystemPasswordResetRequestForm`. | ||
""" | ||
html_email_template_name = {True: 'email/password_reset.html', False: 'email/password_reset_activate.html'} | ||
email_template_name = {True: 'email/password_reset.txt', False: 'email/password_reset_activate.txt'} | ||
|
@@ -461,6 +461,7 @@ def post(self, request, *args, **kwargs): | |
context = { | ||
'site_name': config.site_name, | ||
'ENV': settings.ENVIRONMENT, | ||
'RICH_ENVELOPE': getattr(settings, 'EMAIL_RICH_ENVELOPES', None), | ||
'subject_prefix': settings.EMAIL_SUBJECT_PREFIX_FULL, | ||
'url': url, | ||
'url_first': url[:url.rindex('/')+1], | ||
|
@@ -781,11 +782,19 @@ class MassMailView(AuthMixin, generic.FormView): | |
form_class = MassMailForm | ||
display_permission_denied = False | ||
exact_role = AuthRole.ADMIN | ||
# Keep the email address separate from the one used for transactional | ||
# emails, for better email sender reputation. | ||
mailing_address = '[email protected]' | ||
|
||
def dispatch(self, request, *args, **kwargs): | ||
kwargs['auth_base'] = None | ||
return super().dispatch(request, *args, **kwargs) | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super().get_context_data(**kwargs) | ||
context['mailing_address'] = self.mailing_address | ||
return context | ||
|
||
def get_success_url(self): | ||
return format_lazy( | ||
'{success_url}?nb={sent}', | ||
|
@@ -800,7 +809,7 @@ def form_valid(self, form): | |
preheader = form.cleaned_data['preheader'] | ||
heading = form.cleaned_data['heading'] | ||
category = form.cleaned_data['categories'] | ||
default_from = settings.DEFAULT_FROM_EMAIL | ||
default_from = f'Pasporta Servo <{self.mailing_address}>' | ||
template = get_template('email/mass_email.html') | ||
|
||
opening = make_aware(datetime(2014, 11, 24)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{% load i18n %}{% autoescape off %} | ||
|
||
{% if RICH_ENVELOPE %}[[PLACE-DETAILS]]{% endif %} | ||
|
||
{{ subject_prefix }}{% trans "You received an Authorization" context "Email subject" %} | ||
|
||
{% endautoescape %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.