Emails: use templates for all emails - To be tested !
authorJordan Augé <jordan.auge@lip6.fr>
Fri, 7 Mar 2014 10:57:35 +0000 (11:57 +0100)
committerJordan Augé <jordan.auge@lip6.fr>
Fri, 7 Mar 2014 10:57:35 +0000 (11:57 +0100)
14 files changed:
portal/accountview.py
portal/actions.py
portal/contactview.py
portal/django_passresetview.py
portal/forms.py
portal/joinview.py
portal/manageuserview.py
portal/models.py
portal/registrationview.py
portal/templates/authority_request_email.html [new file with mode: 0644]
portal/templates/authority_request_email.txt [new file with mode: 0644]
portal/templates/authority_request_email_subject.txt [new file with mode: 0644]
portal/templates/contact_support_email.txt [new file with mode: 0644]
portal/templates/contact_support_email_subject.txt [new file with mode: 0644]

index 5d3c9f8..94adaf4 100644 (file)
@@ -10,7 +10,6 @@ from ui.topmenu                         import topmenu_items_live, the_user
 from django.http                        import HttpResponse, HttpResponseRedirect
 from django.contrib                     import messages
 from django.contrib.auth.decorators     import login_required
-from django.core.mail                   import send_mail
 
 from theme import ThemeView
 
index e39d480..7d7a337 100644 (file)
@@ -6,7 +6,7 @@ import json
 
 from django.contrib.auth.models import User
 from django.template.loader     import render_to_string
-from django.core.mail           import send_mail,EmailMultiAlternatives
+from django.core.mail           import EmailMultiAlternatives
 
 from theme                      import ThemeView
 
@@ -399,25 +399,27 @@ def create_pending_slice(wsgi_request, request, email):
     )
     s.save()
 
-    # Send an email: the recipients are the PI of the authority
-    recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn'])
-
-    theme.template_name = 'slice_request_email.txt' 
-    text_content = render_to_string(theme.template, request)
-
-    theme.template_name = 'slice_request_email.html' 
-    html_content = render_to_string(theme.template, request)
-
-    theme.template_name = 'slice_request_email_subject.txt'
-    subject = render_to_string(theme.template, request)
-    subject = subject.replace('\n', '')
-
-    sender = email
-    msg = EmailMultiAlternatives(subject, text_content, sender, [recipients])
-    print msg
-    msg.attach_alternative(html_content, "text/html")
-    msg.send()
-    #send_mail(subject, msg, email, recipients)
+    try:
+        # Send an email: the recipients are the PI of the authority
+        recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn'])
+    
+        theme.template_name = 'slice_request_email.txt' 
+        text_content = render_to_string(theme.template, request)
+    
+        theme.template_name = 'slice_request_email.html' 
+        html_content = render_to_string(theme.template, request)
+    
+        theme.template_name = 'slice_request_email_subject.txt'
+        subject = render_to_string(theme.template, request)
+        subject = subject.replace('\n', '')
+    
+        sender = email
+        msg = EmailMultiAlternatives(subject, text_content, sender, [recipients])
+        print msg
+        msg.attach_alternative(html_content, "text/html")
+        msg.send()
+    except Exception, e:
+        print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
 
 #-------------------------------------------------------------------------------
 # REQUESTS - Users
@@ -572,24 +574,27 @@ def create_pending_user(wsgi_request, request, user_detail):
     except Exception, e:
         print "Failed creating manifold account on platform %s for user: %s" % ('myslice', request['email'])
 
-    # Send an email: the recipients are the PI of the authority
-    # If No PI is defined for this Authority, send to a default email (different for each theme)
-    recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn'])
+    try:
+        # Send an email: the recipients are the PI of the authority
+        # If No PI is defined for this Authority, send to a default email (different for each theme)
+        recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn'])
+        
+        theme.template_name = 'user_request_email.html'
+        html_content = render_to_string(theme.template, request)
     
-    theme.template_name = 'user_request_email.html'
-    html_content = render_to_string(theme.template, request)
-
-    theme.template_name = 'user_request_email.txt'
-    text_content = render_to_string(theme.template, request)
-
-    theme.template_name = 'user_request_email_subject.txt'
-    subject = render_to_string(theme.template, request)
-    subject = subject.replace('\n', '')
-
-    theme.template_name = 'email_default_sender.txt'
-    sender =  render_to_string(theme.template, request)
-    sender = sender.replace('\n', '')
-
-    msg = EmailMultiAlternatives(subject, text_content, sender, [recipients])
-    msg.attach_alternative(html_content, "text/html")
-    msg.send()
+        theme.template_name = 'user_request_email.txt'
+        text_content = render_to_string(theme.template, request)
+    
+        theme.template_name = 'user_request_email_subject.txt'
+        subject = render_to_string(theme.template, request)
+        subject = subject.replace('\n', '')
+    
+        theme.template_name = 'email_default_sender.txt'
+        sender =  render_to_string(theme.template, request)
+        sender = sender.replace('\n', '')
+    
+        msg = EmailMultiAlternatives(subject, text_content, sender, [recipients])
+        msg.attach_alternative(html_content, "text/html")
+        msg.send()
+    except Exception, e:
+        print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
index 63b4bb0..13450df 100644 (file)
@@ -1,7 +1,6 @@
 from django.shortcuts           import render
 from django.template.loader     import render_to_string
 from django.views.generic       import View
-from django.core.mail           import send_mail
 
 from unfold.loginrequired       import FreeAccessView
 from ui.topmenu                 import topmenu_items, the_user
@@ -26,13 +25,37 @@ class ContactView (FreeAccessView, ThemeView):
             email = form.cleaned_data['email'] # email of the sender
             cc_myself = form.cleaned_data['cc_myself']
 
-            #recipients = authority_get_pi_emails(authority_hrn)
-            recipients = ['support@myslice.info' ]
-            if cc_myself:
-                recipients.append(email)
+            try:
+                # Send an email: the support recipients
+                theme.template_name = 'email_support.txt'
+                recipients = render_to_string(theme.template, form.cleaned_data)
+                recipients = subject.replace('\n', '')
+                if cc_myself:
+                    recipients.append(email)
+        
+                theme.template_name = 'contact_support_email.html'
+                html_content = render_to_string(theme.template, form.cleaned_data)
+        
+                theme.template_name = 'contact_support_email.txt'
+                text_content = render_to_string(theme.template, form.cleaned_data)
+        
+                theme.template_name = 'contact_support_email_subject.txt'
+                subject = render_to_string(theme.template, form.cleaned_data)
+                subject = subject.replace('\n', '')
+        
+                if not email:
+                    theme.template_name = 'email_default_sender.txt'
+                    sender =  render_to_string(theme.template, form.cleaned_data)
+                    sender = sender.replace('\n', '')
+                else:
+                    sender = email
+        
+                msg = EmailMultiAlternatives(subject, text_content, sender, [recipients])
+                msg.attach_alternative(html_content, "text/html")
+                msg.send()
+            except Exception, e:
+                print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
 
-            msg = render_to_string('contact-support-email.txt', form.cleaned_data)
-            send_mail("Onelab user %s submitted a query "%email, msg, email, recipients)
             if request.user.is_authenticated() :
                 username = request.user.email
             else :
index d931ccf..7d7f52e 100644 (file)
@@ -72,7 +72,6 @@ import json
 
 from random                     import choice
 
-from django.core.mail           import send_mail
 from django.contrib             import messages
 from django.views.generic       import View
 from django.shortcuts           import render
index 61ffb86..cf3e172 100644 (file)
@@ -170,28 +170,31 @@ class PasswordResetForm(forms.Form):
         Generates a one-use only link for resetting password and sends to the
         user.
         """
-        from django.core.mail import send_mail
-        for user in self.users_cache:
-            if not domain_override:
-                current_site = get_current_site(request)
-                site_name = current_site.name
-                domain = current_site.domain
-            else:
-                site_name = domain = domain_override
-            c = {
-                'email': user.email,
-                'domain': domain,
-                'site_name': site_name,
-                'uid': int_to_base36(user.pk),
-                'user': user,
-                'token': token_generator.make_token(user),
-                'protocol': use_https and 'https' or 'http',
-            }
-            subject = loader.render_to_string(subject_template_name, c)
-            # Email subject *must not* contain newlines
-            subject = ''.join(subject.splitlines())
-            email = loader.render_to_string(email_template_name, c)
-            send_mail(subject, email, from_email, [user.email])
+        from django.core.mail import send_mail,EmailMultiAlternatives
+        try:        
+            for user in self.users_cache:
+                if not domain_override:
+                    current_site = get_current_site(request)
+                    site_name = current_site.name
+                    domain = current_site.domain
+                else:
+                    site_name = domain = domain_override
+                c = {
+                    'email': user.email,
+                    'domain': domain,
+                    'site_name': site_name,
+                    'uid': int_to_base36(user.pk),
+                    'user': user,
+                    'token': token_generator.make_token(user),
+                    'protocol': use_https and 'https' or 'http',
+                }
+                subject = loader.render_to_string(subject_template_name, c)
+                # Email subject *must not* contain newlines
+                subject = ''.join(subject.splitlines())
+                email = loader.render_to_string(email_template_name, c)
+                send_mail(subject, email, from_email, [user.email])
+        except Exception, e:
+            print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
 
 
 class SetPasswordForm(forms.Form):
index caea6d2..abe73f3 100644 (file)
@@ -2,7 +2,7 @@ import os.path, re
 import json
 from random import randint
 
-from django.core.mail           import send_mail
+from django.core.mail           import EmailMultiAlternatives
 from django.contrib.auth.models import User
 from django.views.generic       import View
 from django.template.loader     import render_to_string
@@ -161,25 +161,57 @@ class JoinView (FreeAccessView, ThemeView):
                 user_id = user_detail['user_id']+1 # the user_id for the newly created user in local:user
                 account_params = {'platform_id': 5, 'user_id': user_id, 'auth_type': auth_type, 'config': account_config}
                 manifold_add_account(request,account_params)
+
                 # Send email
-                ctx = {
-                    'first_name'    : reg_fname, 
-                    'last_name'     : reg_lname, 
-                    'authority_hrn' : reg_auth,
-                    'email'         : reg_email,
-                    'user_hrn'      : user_hrn,
-                    'public_key'    : public_key,
-                    }
-                recipients = authority_get_pi_emails(request,reg_auth)
-                
-                # We don't need to send this email to user.
-                # it's for the PI only
-                #if ctx['cc_myself']:
-                #    recipients.append(ctx['email'])
-
-                msg = render_to_string('user_request_email.txt', ctx)
-                send_mail("Onelab New Authority request for %s submitted"%reg_email, msg, 'support@myslice.info', recipients)
+                try: 
+                    ctx = {
+                        'site_name'             : reg_site_name,             
+                        'authority_hrn'         : reg_root_authority_hrn + '.' + reg_site_authority,
+                        'site_abbreviated_name' : reg_site_abbreviated_name, 
+                        'site_url'              : reg_site_url,
+                        'site_latitude'         : reg_site_latitude, 
+                        'site_longitude'        : reg_site_longitude,
+                        'address_line1'         : reg_address_line1,
+                        'address_line2'         : reg_address_line2,
+                        'address_line3'         : reg_address_line3,
+                        'address_city'          : reg_address_city,
+                        'address_postalcode'    : reg_address_postalcode,
+                        'address_state'         : reg_address_state,
+                        'address_country'       : reg_address_country,
+                        'first_name'            : reg_fname, 
+                        'last_name'             : reg_lname, 
+                        'authority_hrn'         : reg_auth,
+                        'email'                 : reg_email,
+                        'user_hrn'              : user_hrn,
+                        'public_key'            : public_key,
+                        }
+                    recipients = authority_get_pi_emails(request,reg_auth)
+                    
+                    # We don't need to send this email to user.
+                    # it's for the PI only
+                    #if ctx['cc_myself']:
+                    #    recipients.append(ctx['email'])
+                    theme.template_name = 'authority_request_email.html'
+                    html_content = render_to_string(theme.template, ctx)
+            
+                    theme.template_name = 'authority_request_email.txt'
+                    text_content = render_to_string(theme.template, ctx)
+            
+                    theme.template_name = 'authority_request_email_subject.txt'
+                    subject = render_to_string(theme.template, ctx)
+                    subject = subject.replace('\n', '')
+            
+                    theme.template_name = 'email_default_sender.txt'
+                    sender =  render_to_string(theme.template, ctx)
+                    sender = sender.replace('\n', '')
+            
+                    msg = EmailMultiAlternatives(subject, text_content, sender, [recipients])
+                    msg.attach_alternative(html_content, "text/html")
+                    msg.send()
+    
+                except Exception, e:
+                    print "Failed to send email, please check the mail templates and the SMTP configuration of your server"
+
                 return render(request, 'user_register_complete.html') 
 
         template_env = {
index 4f5c72f..5b8d2db 100644 (file)
@@ -10,7 +10,6 @@ from ui.topmenu                         import topmenu_items_live, the_user
 from django.http                        import HttpResponse, HttpResponseRedirect
 from django.contrib                     import messages
 from django.contrib.auth.decorators     import login_required
-from django.core.mail                   import send_mail
 from theme import ThemeView
 #
 import json, os, re, itertools
index 0ea0282..3fa0383 100644 (file)
@@ -26,7 +26,6 @@ import random
 import re
 
 from django.conf              import settings
-from django.core.mail         import send_mail
 from django.db                import models
 from django.db                import transaction
 from django.utils.translation import ugettext_lazy as _
index 44bcdd6..9f5f39f 100644 (file)
@@ -2,7 +2,6 @@ import os.path, re
 import json
 from random import randint
 
-from django.core.mail           import send_mail
 from django.views.generic       import View
 from django.template.loader     import render_to_string
 from django.shortcuts           import render
diff --git a/portal/templates/authority_request_email.html b/portal/templates/authority_request_email.html
new file mode 100644 (file)
index 0000000..51b7c40
--- /dev/null
@@ -0,0 +1,27 @@
+<img src="https://onelab.eu/templates/onelab2/images/logo.png">
+<br>
+<h1>NEW AUTHORITY REQUEST</h1>
+<br>
+<h2>{{site_name}}</h2>             
+<b>Authority hrn    :</b> {{authority_hrn}}         
+<b>Abreviated name  :</b> {{site_abbreviated_name}} 
+<b>URL              :</b> {{site_url}}              
+<b>latitude         :</b> {{site_latitude}}         
+<b>longitude        :</b> {{site_longitude}}        
+<b>Address          :</b> {{address_line1}}<br>        
+                          {{address_line2}}<br>     
+                          {{address_line3}}<br>         
+<b>City             :</b> {{address_city}}          
+<b>Postcode         :</b> {{address_postalcode}}    
+<b>State            :</b> {{address_state}}         
+<b>Country          :</b> {{address_country}}  
+
+<br><br>
+
+<h2>PI for this Authority</h2>     
+<b>First Name   :</b> {{first_name}}
+<b>Last Name    :</b> {{last_name}}
+<b>Authority hrn:</b> {{authority_hrn}}
+<b>Public key   :</b> {{public_key}}
+<b>Email        :</b> {{email}}
+<b>User Hrn     :</b> {{user_hrn}}
diff --git a/portal/templates/authority_request_email.txt b/portal/templates/authority_request_email.txt
new file mode 100644 (file)
index 0000000..25eb21b
--- /dev/null
@@ -0,0 +1,27 @@
+NEW AUTHORITY REQUEST
+
+Site             : {{site_name}}             
+
+Authority hrn    : {{authority_hrn}}         
+Abreviated name  : {{site_abbreviated_name}} 
+URL              : {{site_url}}              
+
+latitude         : {{site_latitude}}         
+longitude        : {{site_longitude}}        
+
+Address          : {{address_line1}}
+                   {{address_line2}}     
+                   {{address_line3}}         
+City             : {{address_city}}          
+Postcode         : {{address_postalcode}}    
+State            : {{address_state}}         
+Country          : {{address_country}}  
+
+
+PI for this Authority     
+First Name   : {{first_name}}
+Last Name    : {{last_name}}
+Authority hrn: {{authority_hrn}}
+Public key   : {{public_key}}
+Email        : {{email}}
+User Hrn     : {{user_hrn}}
diff --git a/portal/templates/authority_request_email_subject.txt b/portal/templates/authority_request_email_subject.txt
new file mode 100644 (file)
index 0000000..226af29
--- /dev/null
@@ -0,0 +1 @@
+OneLab new Authority request submitted
diff --git a/portal/templates/contact_support_email.txt b/portal/templates/contact_support_email.txt
new file mode 100644 (file)
index 0000000..9eb576d
--- /dev/null
@@ -0,0 +1,12 @@
+User details: 
+
+First name      : {{first_name}}
+Last name      : {{last_name}}
+email           : {{email}}
+Authority      : {{authority}}
+
+Query Details:
+
+Subject         : {{subject}}
+Description     : {{description}}
+
diff --git a/portal/templates/contact_support_email_subject.txt b/portal/templates/contact_support_email_subject.txt
new file mode 100644 (file)
index 0000000..dd21e44
--- /dev/null
@@ -0,0 +1 @@
+OneLab support: user ticket