diff options
author | Prabhu Ramachandran | 2017-08-24 11:14:41 +0530 |
---|---|---|
committer | GitHub | 2017-08-24 11:14:41 +0530 |
commit | 13b087846f66f450ad1ed12fd534ae2b30cddbcf (patch) | |
tree | dda6c233926e2518d57c3a11d8b647409cb05618 /yaksh/send_emails.py | |
parent | 5bf07392b04e1ad92b9b96dcaa932598d48be2b5 (diff) | |
parent | d6c4d44dca8520926fd781b87e35ecd7843e515c (diff) | |
download | online_test-13b087846f66f450ad1ed12fd534ae2b30cddbcf.tar.gz online_test-13b087846f66f450ad1ed12fd534ae2b30cddbcf.tar.bz2 online_test-13b087846f66f450ad1ed12fd534ae2b30cddbcf.zip |
Merge pull request #295 from adityacp/bulk_email
Send emails to students enrolled in a course
Diffstat (limited to 'yaksh/send_emails.py')
-rw-r--r-- | yaksh/send_emails.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/yaksh/send_emails.py b/yaksh/send_emails.py index 24215dd..ae49f23 100644 --- a/yaksh/send_emails.py +++ b/yaksh/send_emails.py @@ -7,11 +7,14 @@ from string import digits, punctuation import hashlib 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): @@ -57,3 +60,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 = "" + 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 |