From f6dea00d882b0a3687141f212b0a1199b1a943a5 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Sun, 3 Mar 2013 08:05:55 +0100 Subject: [PATCH] =?UTF-8?q?[utils/sendmail.py]=20Pour=20envoyer=20des=20ma?= =?UTF-8?q?ils=20en=20python=20en=20g=C3=A9rant=20correctement=20l'encodag?= =?UTF-8?q?e=20et=20les=20headers.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/sendmail.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 utils/sendmail.py diff --git a/utils/sendmail.py b/utils/sendmail.py new file mode 100644 index 00000000..76dc5b88 --- /dev/null +++ b/utils/sendmail.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""API pour envoyer facilement un mail en python + Author: Vincent Le Gallic + + """ + +# Librairie de communication SMTP +import smtplib +from email.mime.text import MIMEText +from email import Charset +Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8') + +def sendmail(emetteur, destinataires, objet, message, cc=[], more_headers={}, debug=False): + """Envoie un mail en utilisant la commande sendmail""" + mail = MIMEText(message, "plain", "utf-8") + mail["From"] = emetteur + mail["To"] = u", ".join(destinataires) + mail["Subject"] = objet + more_headers["X-Mailer"] = more_headers.get("X-Mailer", "/usr/scripts/utils/sendmail.py") + for k in more_headers.keys(): + mail[k] = more_headers[k] + if cc != []: + mail["Cc"] = u", ".join(cc) + if debug: + print mail.as_string() + else: + s = smtplib.SMTP('smtp.adm.crans.org') + s.sendmail(emetteur, destinataires, mail.as_string()) + s.quit() + + +if __name__ == "__main__": + print "Exemple d'utilisation :" + print ">>> import sendmail" + print '>>> sendmail.sendmail("passoire@crans.org", ["nobody@crans.org", "root@crans.org"], "[Test] Envoi de mail", "Ceci est un message envoyé par un script.\\n-- \\nsendmail.py")'