From bbaa371461ca2149312c732f79b8960b0b53a73b Mon Sep 17 00:00:00 2001 From: Romain Beuque Date: Fri, 5 Sep 2014 13:40:31 +0200 Subject: [PATCH] =?UTF-8?q?syst=C3=A8me=20de=20mail=20fonctionnel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Brie/brie/lib/smtp_helper.py | 44 ++++++++++++++++++++++++++++ Brie/brie/plugins/wifi/controller.py | 27 +++++------------ 2 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 Brie/brie/lib/smtp_helper.py diff --git a/Brie/brie/lib/smtp_helper.py b/Brie/brie/lib/smtp_helper.py new file mode 100644 index 0000000..ec9297b --- /dev/null +++ b/Brie/brie/lib/smtp_helper.py @@ -0,0 +1,44 @@ +#coding: utf-8 +from cStringIO import StringIO +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.header import Header +from email import Charset +from email.generator import Generator +import smtplib + +class SmtpHelper: + + @staticmethod + def send_email(from_address, recipient, subject, text): + # Default encoding mode set to Quoted Printable. Acts globally! + Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8') + + # 'alternative’ MIME type – HTML and plain text bundled in one e-mail message + msg = MIMEMultipart('alternative') + msg['Subject'] = "%s" % Header(subject, 'utf-8') + # Only descriptive part of recipient and sender shall be encoded, not the email address + msg['From'] = "\"%s\" <%s>" % (Header(from_address[0], 'utf-8'), from_address[1]) + msg['To'] = "\"%s\" <%s>" % (Header(recipient[0], 'utf-8'), recipient[1]) + + # Attach both parts + textpart = MIMEText(text, 'plain', 'UTF-8') + msg.attach(textpart) + + # Create a generator and flatten message object to 'file’ + str_io = StringIO() + g = Generator(str_io, False) + g.flatten(msg) + # str_io.getvalue() contains ready to sent message + + # Optionally - send it – using python's smtplib + # or just use Django's + + smtpObj = smtplib.SMTP('smtp.u-psud.fr') + smtpObj.sendmail(from_address[1], recipient[1], str_io.getvalue()) + #except SMTPException: + # print "Error: unable to send email" + # raise Exception("Mail non envoyé") + + #end def +#end class \ No newline at end of file diff --git a/Brie/brie/plugins/wifi/controller.py b/Brie/brie/plugins/wifi/controller.py index 2e9cebb..20dc8b7 100644 --- a/Brie/brie/plugins/wifi/controller.py +++ b/Brie/brie/plugins/wifi/controller.py @@ -6,13 +6,13 @@ from brie.config import ldap_config, groups_enum from brie.lib.ldap_helper import * from brie.lib.aurore_helper import * from brie.lib.name_translation_helpers import Passwords +from brie.lib.smtp_helper import SmtpHelper from brie.model.ldap import * from brie.model.ldap import Wifi as WifiModel from brie.controllers import auth from brie.controllers.auth import AuthenticatedBaseController, AuthenticatedRestController -import smtplib class Wifi: @@ -71,26 +71,13 @@ class DirectController(AuthenticatedRestController): self.user.ldap_bind.save(wifi) #end - sender = "noreply@fede-aurore.net" - receivers = [member.mail.first()] + # Envoi du mail + from_address = [u'Fédération Aurore', 'noreply@fede-aurore.net'] + recipient = [member.cn.first(), member.mail.first()] + subject = u'['+ residence +'] votre mot de passe WiFi' + text = u'Bonjour,\n\nVous venez de vous inscrire au sein d\'une résidence de la fédération Aurore\nUn mot de passe pour utiliser la connexion WiFi de la résidence vous a été assigné.\n\nUtilisateur: '+ member_uid +'\nMot de passe: '+ password +u'\n\nCordialement,\nla fédération Aurore' - message = """From: Federation Aurore -To: """ + member.cn.first().decode("utf-8").encode("ascii", "ignore") + """ <""" + member.mail.first().decode("utf-8").encode("ascii", "ignore") + """> -Subject: Votre mot de passe WiFi : residence universitaire - - Bienvenue dans votre residence etudiante de Paris Sud - - Votre nom de connexion est : """ + member_uid + """ - Votre mot de passe WiFi est : """ + password - - message = message.encode("utf-8") - - try: - smtpObj = smtplib.SMTP('smtp.u-psud.fr') - smtpObj.sendmail(sender, receivers, message) - print "Successfully sent email" - except SMTPException: - print "Error: unable to send email" + SmtpHelper.send_email(from_address, recipient, subject, text) redirect("/show/member/" + residence + "/" + member_uid) #end def