système de mail fonctionnel

This commit is contained in:
Romain Beuque 2014-09-05 13:40:31 +02:00
parent 291e1e51c3
commit bbaa371461
2 changed files with 51 additions and 20 deletions

View File

@ -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

View File

@ -6,13 +6,13 @@ from brie.config import ldap_config, groups_enum
from brie.lib.ldap_helper import * from brie.lib.ldap_helper import *
from brie.lib.aurore_helper import * from brie.lib.aurore_helper import *
from brie.lib.name_translation_helpers import Passwords 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 *
from brie.model.ldap import Wifi as WifiModel from brie.model.ldap import Wifi as WifiModel
from brie.controllers import auth from brie.controllers import auth
from brie.controllers.auth import AuthenticatedBaseController, AuthenticatedRestController from brie.controllers.auth import AuthenticatedBaseController, AuthenticatedRestController
import smtplib
class Wifi: class Wifi:
@ -71,26 +71,13 @@ class DirectController(AuthenticatedRestController):
self.user.ldap_bind.save(wifi) self.user.ldap_bind.save(wifi)
#end #end
sender = "noreply@fede-aurore.net" # Envoi du mail
receivers = [member.mail.first()] 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 <noreply@fede-aurore.net> SmtpHelper.send_email(from_address, recipient, subject, text)
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"
redirect("/show/member/" + residence + "/" + member_uid) redirect("/show/member/" + residence + "/" + member_uid)
#end def #end def