diff options
author | Akshen | 2017-06-06 12:08:37 +0530 |
---|---|---|
committer | Akshen | 2017-06-06 12:08:37 +0530 |
commit | 863595a24036f0026fc8079f9978e40c2f36d792 (patch) | |
tree | cde48ca816258d193bfa4af75dd55a634d5c9f23 | |
parent | 8e59e5ad6ff598f71168ec1a832a17ef9c52b8a3 (diff) | |
download | workshop_booking-863595a24036f0026fc8079f9978e40c2f36d792.tar.gz workshop_booking-863595a24036f0026fc8079f9978e40c2f36d792.tar.bz2 workshop_booking-863595a24036f0026fc8079f9978e40c2f36d792.zip |
Fixes Email Attachment using django-email
- Sender Email added in local_settings
- Template fix for Proposed_workshop
-rw-r--r-- | local_settings.py | 1 | ||||
-rw-r--r-- | workshop_app/send_mails.py | 82 | ||||
-rw-r--r-- | workshop_app/templates/workshop_app/my_workshops.html | 4 | ||||
-rw-r--r-- | workshop_portal/settings.py | 7 |
4 files changed, 74 insertions, 20 deletions
diff --git a/local_settings.py b/local_settings.py index 8251c89..940a33e 100644 --- a/local_settings.py +++ b/local_settings.py @@ -7,3 +7,4 @@ EMAIL_PORT = 'PORT Number' EMAIL_HOST_USER = 'your username' EMAIL_HOST_PASSWORD = 'your password' EMAIL_USE_TLS = True +SENDER_EMAIL = 'your email address'
\ No newline at end of file diff --git a/workshop_app/send_mails.py b/workshop_app/send_mails.py index a8ada76..0c9c97e 100644 --- a/workshop_app/send_mails.py +++ b/workshop_app/send_mails.py @@ -16,8 +16,10 @@ from workshop_portal.settings import ( EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, EMAIL_USE_TLS, - PRODUCTION_URL + PRODUCTION_URL, + SENDER_EMAIL ) +from django.core.mail import EmailMultiAlternatives def generate_activation_key(username): """Generates hashed secret key for email activation""" @@ -101,7 +103,7 @@ def send_email( request, call_on, try: send_mail( "Instructor Registration - FOSSEE, IIT Bombay", message, - EMAIL_HOST_USER, [request.user.email], fail_silently=False + SENDER_EMAIL, [request.user.email], fail_silently=False ) except Exception: send_smtp_email(request=request, @@ -122,7 +124,7 @@ def send_email( request, call_on, try: send_mail("New Instructor Registration - FOSSEE, IIT Bombay", - message, EMAIL_HOST_USER, ['workshops@fossee.in'], + message, SENDER_EMAIL, ['workshops@fossee.in'], fail_silently=False) except Exception: send_smtp_email(request=request, @@ -147,7 +149,7 @@ def send_email( request, call_on, try: send_mail( - "Coordinator Registration at FOSSEE, IIT Bombay", message, EMAIL_HOST_USER, + "Coordinator Registration at FOSSEE, IIT Bombay", message, SENDER_EMAIL, [request.user.email], fail_silently=False ) except Exception: @@ -167,17 +169,18 @@ def send_email( request, call_on, Workshop title:{5} You may accept or reject this booking - workshop.fossee.in .""".format( + {6}/my_workshops/ .""".format( user_name, request.user.email, request.user.profile.phone_number, request.user.profile.institute, - workshop_date, workshop_title + workshop_date, workshop_title, + PRODUCTION_URL )) try: send_mail( "New FOSSEE Workshop booking on {0}".format(workshop_date), - message, EMAIL_HOST_USER, [other_email], + message, SENDER_EMAIL, [other_email], fail_silently=False ) except Exception: @@ -195,17 +198,17 @@ def send_email( request, call_on, Your request has been received and is awaiting instructor approval/disapproval. You will be notified about the status - via email and on your workshops.fossee.in + via email and on {2}/my_workshops/ In case of queries regarding workshop booking(s), revert to this email.""".format( - workshop_date, workshop_title + workshop_date, workshop_title, PRODUCTION_URL )) try: send_mail( "Pending Request for New FOSSEE Workshop booking on {0}" - .format(workshop_date), message, EMAIL_HOST_USER, + .format(workshop_date), message, SENDER_EMAIL, [request.user.email], fail_silently=False ) except Exception: @@ -229,11 +232,34 @@ def send_email( request, call_on, been sent to the coordinator. """.format(user_name, other_email, phone_number, institute, workshop_date, workshop_title)) + + subject = "FOSSEE Workshop booking confirmation on {0}".\ + format(workshop_date) + msg = EmailMultiAlternatives(subject, message, SENDER_EMAIL, [request.user.email]) + from django.conf import settings + from os import listdir, path + from email.mime.multipart import MIMEMultipart + from email.mime.text import MIMEText + from email.mime.base import MIMEBase + from email import encoders + + files = listdir(settings.MEDIA_ROOT) + for f in files: + attachment = open(path.join(settings.MEDIA_ROOT,f), 'rb') + part = MIMEBase('application', 'octet-stream') + part.set_payload((attachment).read()) + encoders.encode_base64(part) + part.add_header('Content-Disposition', "attachment; filename= %s " % f) + msg.attach(part) + msg.send() + + ''' send_smtp_email(request=request, subject="FOSSEE Workshop booking confirmation on {0}". format(workshop_date), message=message, other_email=request.user.email, attachment=1 ) + ''' else: message = dedent("""\ Instructor name:{0} @@ -251,11 +277,35 @@ def send_email( request, call_on, phone_number, workshop_date, workshop_title )) + subject = "FOSSEE Workshop booking confirmation on {0}".\ + format(workshop_date) + msg = EmailMultiAlternatives(subject, message, SENDER_EMAIL, [request.user.email]) + from django.conf import settings + from os import listdir, path + from email.mime.multipart import MIMEMultipart + from email.mime.text import MIMEText + from email.mime.base import MIMEBase + from email import encoders + + files = listdir(settings.MEDIA_ROOT) + for f in files: + attachment = open(path.join(settings.MEDIA_ROOT,f), 'rb') + part = MIMEBase('application', 'octet-stream') + part.set_payload((attachment).read()) + encoders.encode_base64(part) + part.add_header('Content-Disposition', "attachment; filename= %s " % f) + msg.attach(part) + msg.send() + + + + ''' send_smtp_email(request=request, subject="FOSSEE Workshop booking confirmation on {0}". format(workshop_date), message=message, other_email=other_email, attachment=1 ) + ''' elif call_on == "Booking Request Rejected": if user_position == "instructor": @@ -274,7 +324,7 @@ def send_email( request, call_on, try: send_mail("FOSSEE Workshop booking rejected for {0}" - .format(workshop_date), message, EMAIL_HOST_USER, + .format(workshop_date), message, SENDER_EMAIL, [request.user.email], fail_silently=False) except Exception: send_smtp_email(request=request, @@ -290,12 +340,12 @@ def send_email( request, call_on, We regret to inform you that your workshop booking has been rejected due to unavailability of the instructor. You may try booking other available - slots workshops.fossee.in """ - .format(workshop_date, workshop_title)) + slots {2}/book/ """ + .format(workshop_date, workshop_title, PRODUCTION_URL)) try: send_mail("FOSSEE Workshop booking rejected for {0}". - format(workshop_date), message, EMAIL_HOST_USER, + format(workshop_date), message, SENDER_EMAIL, [other_email], fail_silently=False) except Exception: send_smtp_email(request=request, @@ -313,7 +363,7 @@ def send_email( request, call_on, .format(workshop_date, workshop_title)) try: send_mail("FOSSEE workshop deleted for {0}".format(workshop_date), - message, EMAIL_HOST_USER, [request.user.email], + message, SENDER_EMAIL, [request.user.email], fail_silently=False) except Exception: send_smtp_email(request=request, @@ -325,7 +375,7 @@ def send_email( request, call_on, else: message = "Issue at Workshop Booking App please check" try: - send_mail("Issue At Workshop Booking App Mailing", message, EMAIL_HOST_USER, + send_mail("Issue At Workshop Booking App Mailing", message, SENDER_EMAIL, ['doke.akshen@gmail.com', 'mahesh.p.gudi@gmail.com', 'aditya94palaparthy@gmail.com'], fail_silently=False) except Exception: send_smtp_email(request=request, diff --git a/workshop_app/templates/workshop_app/my_workshops.html b/workshop_app/templates/workshop_app/my_workshops.html index 275a0a0..c20a6ca 100644 --- a/workshop_app/templates/workshop_app/my_workshops.html +++ b/workshop_app/templates/workshop_app/my_workshops.html @@ -147,7 +147,7 @@ <td>{{ workshop.requested_workshop_date }}</td> <td><span class = "label label-success">{{ workshop.status }}</span></td> {% endif %} - {% if workshop.status == 'ACCEPTED' and workshop.conditionone %} + {% if workshop.status == 'ACCEPTED' and workshop.condition_one %} {% if request.user.profile.position == 'instructor' %} <td>{{ workshop.proposed_workshop_coordinator }}</td> {% else %} @@ -186,7 +186,7 @@ {% for workshop in workshop_occurences %} <tbody> <tr> - {% if workshop.status == 'Pending' and workshop.conditionone %} + {% if workshop.status == 'Pending' and workshop.condition_one %} {% if request.user.profile.position == 'instructor' %} <td>{{ workshop.proposed_workshop_coordinator }}</td> {% endif %} diff --git a/workshop_portal/settings.py b/workshop_portal/settings.py index 91f4b6a..8f33389 100644 --- a/workshop_portal/settings.py +++ b/workshop_portal/settings.py @@ -17,7 +17,8 @@ from local_settings import ( EMAIL_PORT, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, - EMAIL_USE_TLS + EMAIL_USE_TLS, + SENDER_EMAIL ) # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -141,13 +142,15 @@ MEDIA_URL = '/data/' MEDIA_ROOT = os.path.join(BASE_DIR, "workshop_app", "data") -#Email Connection Settings +#Email Connection Settings from local_settings.py EMAIL_HOST = EMAIL_HOST EMAIL_HOST_USER = EMAIL_HOST_USER EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD EMAIL_PORT = EMAIL_PORT EMAIL_USE_TLS = EMAIL_USE_TLS EMAIL_TIMEOUT = 300 +SENDER_EMAIL = SENDER_EMAIL + #EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' #Temp Solution #Change this to the production url |