From db8aa57e9a3caf9b2ed080b440703d634abd14a1 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 15 May 2017 15:00:48 +0530 Subject: Add send_bulk_mail function to send mails to students enrolled in a course --- yaksh/send_emails.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'yaksh/send_emails.py') 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 -- cgit From 57eaf342d16f33b8ea67b89c78907d9c1f7b4632 Mon Sep 17 00:00:00 2001 From: adityacp Date: Wed, 14 Jun 2017 12:33:45 +0530 Subject: Change send_emails.py and course_detail.html - Change variable assignment in send_emails.py - Add proper indentation in course_detail.html --- yaksh/send_emails.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'yaksh/send_emails.py') diff --git a/yaksh/send_emails.py b/yaksh/send_emails.py index 4465ce9..ae49f23 100644 --- a/yaksh/send_emails.py +++ b/yaksh/send_emails.py @@ -63,7 +63,7 @@ def send_user_mail(user_mail, key): def send_bulk_mail(subject, email_body, recipients, attachments): try: - text_msg = "Yaksh" + text_msg = "" msg = EmailMultiAlternatives(subject, text_msg, settings.SENDER_EMAIL, recipients ) -- cgit