Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update feature to send emails to evalai users using amazon ses #1624

Merged
merged 8 commits into from Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 0 additions & 21 deletions apps/web/templates/notification_email.html

This file was deleted.

3 changes: 3 additions & 0 deletions apps/web/templates/notification_email_data.html
Expand Up @@ -37,6 +37,9 @@
<h5><strong><a ui-sref="home" class="dark-link"><img class="auth-logo" src="{% static 'accounts/images/evalai-logo.png' %}"></a></strong></h5>
<br>
<p class="text-med-gray auth-home"> Notify all users about new challenge </p>
{% if error %}
<p class="text-highlight auth-home"> Some error occured. Please try again later! </p>
{% endif %}
</div>
</div>
<div class="row">
Expand Down
51 changes: 27 additions & 24 deletions apps/web/views.py
@@ -1,11 +1,13 @@
import logging
import traceback

from django.contrib.auth.models import User
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.core.mail import EmailMessage
from django.shortcuts import render
from django.template.loader import get_template
from django.template import Context

from email.MIMEImage import MIMEImage
from smtplib import SMTPException

from .models import Team
from .serializers import ContactSerializer, TeamSerializer
Expand All @@ -17,6 +19,8 @@
from rest_framework.response import Response
from rest_framework.throttling import AnonRateThrottle

logger = logging.getLogger(__name__)


def home(request, template_name="index.html"):
"""
Expand Down Expand Up @@ -49,12 +53,10 @@ def notify_users_about_challenge(request):
return render(request, template_name)

elif request.method == 'POST':
template_name = 'notification_email.html'
emails = User.objects.all().exclude(email__isnull=True, email__exact='').values_list('email', flat=True)
htmly = get_template('notification_email.html')
users = User.objects.exclude(email__exact='').values_list('email', flat=True)

subject = request.POST.get('subject')
body = request.POST.get('body')
body_html = request.POST.get('body')

try:
challenge_image = request.FILES['challenge_image']
Expand All @@ -63,27 +65,28 @@ def notify_users_about_challenge(request):

if challenge_image:
image = MIMEImage(challenge_image.read())
image.add_header('Content-ID', '<{}>'.format(challenge_image))

context = Context({'body': body,
'image': challenge_image})
image.add_header('Content-Disposition', 'inline', filename=challenge_image._name)
image.add_header('Content-ID', '{}'.format(challenge_image))

for email in emails:
from_email = settings.EMAIL_HOST_USER
to = [email]
html_content = htmly.render(context)
sender = settings.EMAIL_HOST_USER

msg = EmailMultiAlternatives(subject, html_content, from_email, to)
msg.attach_alternative(html_content, "text/html")
msg.mixed_subtype = 'related'
email = EmailMessage(subject, body_html, sender, users)
email.content_subtype = 'html'

if challenge_image:
msg.attach(image)
if challenge_image:
email.mixed_subtype = 'related'
email.attach(image)

msg.send()
return render(request,
'notification_email_conformation.html',
{'message': 'All the emails are sent successfully!'})
try:
email.send()
return render(request,
'notification_email_conformation.html',
{'message': 'All the emails are sent successfully!'})
except SMTPException:
logger.exception(traceback.format_exc())
return render(request,
'notification_email_data.html',
{'errors': 1})
else:
return render(request, 'error404.html')
else:
Expand Down
1 change: 1 addition & 0 deletions evalai/urls.py
Expand Up @@ -71,6 +71,7 @@
url(r'^api/web/',
include('web.urls',
namespace='web')),
url(r'^email_reporting/', include('django_ses.urls')),
]

# DJANGO-SPAGHETTI-AND-MEATBALLS URLs available during development only.
Expand Down
1 change: 1 addition & 0 deletions requirements/common.txt
Expand Up @@ -6,6 +6,7 @@ djangorestframework==3.5.2
djangorestframework-expiring-authtoken==0.1.4
django-cors-headers==1.3.1
django-rest-auth[with_social]==0.9.2
django-ses==0.8.5
drfdocs==0.0.11
pika==0.10.0
pickleshare==0.7.4
Expand Down
1 change: 1 addition & 0 deletions settings/common.py
Expand Up @@ -59,6 +59,7 @@
'allauth',
'allauth.account',
'corsheaders',
'django_ses',
'import_export',
'rest_auth',
'rest_auth.registration',
Expand Down
4 changes: 3 additions & 1 deletion settings/prod.py
Expand Up @@ -41,6 +41,8 @@
AWS_STORAGE_BUCKET_NAME = "evalai"
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID', '')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY', '')
AWS_SES_REGION_NAME = os.environ.get('AWS_SES_REGION_NAME', '')
AWS_SES_REGION_ENDPOINT = os.environ.get('AWS_SES_REGION_ENDPOINT', '')

# Amazon S3 Configurations
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
Expand All @@ -57,7 +59,7 @@

# Setup Email Backend related settings
DEFAULT_FROM_EMAIL = "noreply@cloudcv.org"
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_BACKEND = "django_ses.SESBackend"
EMAIL_HOST = os.environ.get("EMAIL_HOST", "")
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD", "")
EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER", "")
Expand Down