Commit 0ee0ca1d authored by Stefano Alberto Russo's avatar Stefano Alberto Russo
Browse files

Dropped Sendgrid client in favour of the Django built-in, SMTP-based mail client.

parent 7d27a5b2
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -75,8 +75,10 @@ services:
      #- ROSETTA_WEBAPP_PORT=8080      # Used for the agent, can be internal.
      #- ROSETTA_REGISTRY_HOST=proxy
      #- ROSETTA_REGISTRY_PORT=5000
      #- DJANGO_EMAIL_APIKEY=""
      #- DJANGO_EMAIL_FROM="Rosetta Platform <notifications@rosetta.platform>"
      #- DJANGO_EMAIL_HOST=
      #- DJANGO_EMAIL_HOST_USER=
      #- DJANGO_EMAIL_HOST_PASSWORD=
      #- DJANGO_EMAIL_FROM="Rosetta <notifications@rosetta.platform>"
      #- DJANGO_SECRET_KEY=""
      #- ROSETTA_TASKS_PROXY_HOST=
      #- ROSETTA_TASKS_TUNNEL_HOST=
+7 −8
Original line number Diff line number Diff line
@@ -361,7 +361,6 @@ print(port)

            # Notify the user that the task called back home if using a WMS
            if task.computing.wms:
                if settings.DJANGO_EMAIL_APIKEY:
                logger.info('Agent API sending task ready mail notification to "{}"'.format(task.user.email))
                mail_subject = 'Your Task "{}" is now starting up'.format(task.container.name)
                mail_text = 'Hello,\n\nyour Task "{}" on {} is now starting up. Check logs or connect here: https://{}/tasks/?uuid={}\n\nThe Rosetta notifications bot.'.format(task.container.name, task.computing, settings.ROSETTA_HOST, task.uuid)
+10 −23
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@ import os
import re
import hashlib
import traceback
import hashlib
import json
import requests
import random
@@ -11,6 +10,7 @@ import logging
from collections import namedtuple
import datetime, calendar, pytz
from dateutil.tz import tzoffset
from django.core.mail import send_mail as django_send_mail

from .exceptions import ErrorMessage

@@ -64,28 +64,15 @@ def send_email(to, subject, text):
    # Importing here instead of on top avoids circular dependencies problems when loading booleanize in settings
    from django.conf import settings

    if settings.DJANGO_EMAIL_SERVICE == 'Sendgrid':
        import sendgrid
        from sendgrid.helpers.mail import Email,Content,Mail

        sg = sendgrid.SendGridAPIClient(apikey=settings.DJANGO_EMAIL_APIKEY)
        from_email = Email(settings.DJANGO_EMAIL_FROM)
        to_email = Email(to)
        subject = subject
        content = Content('text/plain', text)
        mail = Mail(from_email, subject, to_email, content)

        try:
            response = sg.client.mail.send.post(request_body=mail.get())

            #logger.debug(response.status_code)
            #logger.debug(response.body)
            #logger.debug(response.headers)
        except Exception as e:
            logger.error(e)

        #logger.debug(response)

    if settings.EMAIL_HOST:
        django_send_mail(
            subject = subject,
            message = text,
            from_email = settings.DEFAULT_FROM_EMAIL,
            recipient_list = [to],
            fail_silently=False,
        )
        logger.debug('Sent email to "{}": "{}" - "{}"'.format(to, subject, text))

def format_exception(e, debug=False):

+10 −5
Original line number Diff line number Diff line
@@ -159,11 +159,16 @@ ROSETTA_HOST = os.environ.get('ROSETTA_HOST', 'localhost')
#  Email settings
#===============================

DJANGO_EMAIL_SERVICE = os.environ.get('DJANGO_EMAIL_SERVICE', 'Sendgrid')
if not DJANGO_EMAIL_SERVICE in ['Sendgrid', None]:
    raise ImproperlyConfigured('Invalid EMAIL_METHOD ("{}")'.format(DJANGO_EMAIL_SERVICE))
DJANGO_EMAIL_FROM = os.environ.get('DJANGO_EMAIL_FROM', 'Rosetta <notifications@rosetta.local')
DJANGO_EMAIL_APIKEY = os.environ.get('DJANGO_EMAIL_APIKEY', None)
# Email settings
EMAIL_BACKEND = os.environ.get('DJANGO_EMAIL_TYPE', 'django.core.mail.backends.smtp.EmailBackend')
EMAIL_HOST = os.environ.get('DJANGO_EMAIL_HOST', None)
EMAIL_PORT = int(os.environ.get('DJANGO_EMAIL_PORT', 587))
EMAIL_USE_TLS = booleanize(os.environ.get('DJANGO_EMAIL_USE_TLS', True))
EMAIL_USE_SSL = booleanize(os.environ.get('DJANGO_EMAIL_USE_SSL', False))
EMAIL_HOST_USER = os.environ.get('DJANGO_EMAIL_HOST_USER', None)
EMAIL_HOST_PASSWORD = os.environ.get('DJANGO_EMAIL_HOST_PASSWORD', None)
DEFAULT_FROM_EMAIL = os.environ.get('DJANGO_EMAIL_FROM', 'Rosetta <notifications@rosetta.platform>')



#===============================
+0 −1
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ pytz==2022.7.1
djangorestframework==3.9.3
django-rest-swagger==2.2.0
dateutils==0.6.6
sendgrid==5.3.0
mozilla-django-oidc==1.2.4
uwsgi==2.0.20
python-magic==0.4.15