Skip to content

Commit

Permalink
enable mailgun and analytics (#1370)
Browse files Browse the repository at this point in the history
* use anymail to send email via mailgun

* adding notification email setting
  • Loading branch information
shanbady authored Aug 8, 2024
1 parent 7c84f89 commit 068e489
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@
"MAILGUN_SENDER_DOMAIN": MAILGUN_SENDER_DOMAIN,
}

NOTIFICATION_EMAIL_BACKEND = get_string(
"MITOPEN_NOTIFICATION_EMAIL_BACKEND", "anymail.backends.test.EmailBackend"
)
# e-mail configurable admins
ADMIN_EMAIL = get_string("MITOPEN_ADMIN_EMAIL", "")
ADMINS = (("Admins", ADMIN_EMAIL),) if ADMIN_EMAIL != "" else ()
Expand Down
18 changes: 12 additions & 6 deletions profiles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
from xml.sax.saxutils import escape as xml_escape

import requests
from anymail.message import AnymailMessage
from bs4 import BeautifulSoup
from django.conf import settings
from django.core import mail
from django.core.files.temp import NamedTemporaryFile
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from PIL import Image

Expand Down Expand Up @@ -414,9 +415,14 @@ def send_email(recipients, subject, content, text_only):
for link in soup.find_all("a"):
link.replace_with(link.attrs["href"])
text_content = soup.get_text().strip()
msg = EmailMultiAlternatives(
subject, text_content, settings.DEFAULT_FROM_EMAIL, recipients
)
if not text_only:
msg.attach_alternative(content, "text/html")
with mail.get_connection(settings.NOTIFICATION_EMAIL_BACKEND) as connection:
msg = AnymailMessage(
subject=subject,
body=text_content,
to=recipients,
from_email=settings.MAILGUN_FROM_EMAIL,
connection=connection,
)
if not text_only:
msg.attach_alternative(content, "text/html")
return msg.send()

0 comments on commit 068e489

Please sign in to comment.