summaryrefslogtreecommitdiff
path: root/yaksh/send_emails.py
diff options
context:
space:
mode:
authoradityacp2017-05-15 15:00:48 +0530
committeradityacp2017-05-15 15:00:48 +0530
commitdb8aa57e9a3caf9b2ed080b440703d634abd14a1 (patch)
tree3f114ee0fa9d20dfa4e5023f8d3a2b2b1e8359fe /yaksh/send_emails.py
parentb6993d79ae2f0d18375555f5ada72b52a3a2e066 (diff)
downloadonline_test-db8aa57e9a3caf9b2ed080b440703d634abd14a1.tar.gz
online_test-db8aa57e9a3caf9b2ed080b440703d634abd14a1.tar.bz2
online_test-db8aa57e9a3caf9b2ed080b440703d634abd14a1.zip
Add send_bulk_mail function to send mails to students enrolled in a course
Diffstat (limited to 'yaksh/send_emails.py')
-rw-r--r--yaksh/send_emails.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/yaksh/send_emails.py b/yaksh/send_emails.py
index 37736d5..bcf633f 100644
--- a/yaksh/send_emails.py
+++ b/yaksh/send_emails.py
@@ -8,11 +8,14 @@ import hashlib
from random import randint
from textwrap import dedent
import smtplib
+import os
# Django imports
from django.utils.crypto import get_random_string
from django.conf import settings
-from django.core.mail import send_mass_mail, send_mail
+from django.core.mail import EmailMultiAlternatives, send_mail
+from django.core.files.storage import default_storage
+from django.core.files.base import ContentFile
def generate_activation_key(username):
@@ -56,3 +59,30 @@ def send_user_mail(user_mail, key):
success = False
return success, msg
+
+def send_bulk_mail(subject, email_body, recipients, attachments):
+ try:
+ text_msg = "Yaksh"
+ msg = EmailMultiAlternatives(subject, text_msg, settings.SENDER_EMAIL,
+ recipients
+ )
+ msg.attach_alternative(email_body, "text/html")
+ if attachments:
+ for file in attachments:
+ path = default_storage.save('attachments/'+file.name,
+ ContentFile(file.read())
+ )
+ msg.attach_file(os.sep.join((settings.MEDIA_ROOT, path)),
+ mimetype="text/html"
+ )
+ default_storage.delete(path)
+ msg.send()
+
+ message = "Email Sent Successfully"
+
+ except Exception as exc_msg:
+ message = """Error: {0}. Please check email address.\
+ If email address is correct then
+ Please contact {1}.""".format(exc_msg, settings.REPLY_EMAIL)
+
+ return message