Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente | Prochaine révision Les deux révisions suivantes | ||
expedition_mail [2015/02/23 20:48] tyrtamos |
expedition_mail [2015/02/23 21:10] tyrtamos |
||
---|---|---|---|
Ligne 41: | Ligne 41: | ||
Après le code, on trouvera ces 4 utilisations détaillées à titre d' | Après le code, on trouvera ces 4 utilisations détaillées à titre d' | ||
- | \\ | + | D' |
<code python> | <code python> | ||
# | # | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
+ | # Python 2.7 | ||
import sys | import sys | ||
Ligne 151: | Ligne 153: | ||
return rep | return rep | ||
</ | </ | ||
+ | |||
+ | \\ | ||
+ | Et voici le même code converti en Python 3, et un peu retouché: | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | # -*- coding: utf-8 -*- | ||
+ | # Python 3 | ||
+ | |||
+ | import sys | ||
+ | import os | ||
+ | |||
+ | from smtplib import SMTP | ||
+ | |||
+ | from email import encoders | ||
+ | from email.mime.text import MIMEText | ||
+ | from email.mime.multipart import MIMEMultipart | ||
+ | from email.mime.base import MIMEBase | ||
+ | from email.utils import formatdate | ||
+ | |||
+ | ############################################################################## | ||
+ | class ServeurSMTP(object): | ||
+ | |||
+ | def __init__(self, | ||
+ | """ | ||
+ | self.adresse = adresse | ||
+ | self.port = port | ||
+ | self.login = login | ||
+ | self.mdpasse = mdpasse | ||
+ | |||
+ | ############################################################################## | ||
+ | class MessageSMTP(object): | ||
+ | |||
+ | def __init__(self, | ||
+ | """ | ||
+ | |||
+ | # | ||
+ | # correction des arguments selon leur type | ||
+ | |||
+ | self.expediteur = exped | ||
+ | |||
+ | if isinstance(to, | ||
+ | to = to.split(';' | ||
+ | |||
+ | if to == [] or to == ['' | ||
+ | raise ValueError(" | ||
+ | |||
+ | if isinstance(cc, | ||
+ | cc = cc.split(';' | ||
+ | |||
+ | if isinstance(bcc, | ||
+ | bcc = bcc.split(';' | ||
+ | |||
+ | if isinstance(pjointes, | ||
+ | pjointes = pjointes.split(';' | ||
+ | |||
+ | if codage == None or codage == "": | ||
+ | codage = ' | ||
+ | |||
+ | # | ||
+ | # construction du mail à envoyer (en-tête + corps) | ||
+ | |||
+ | if pjointes == []: | ||
+ | # message sans pièce jointe | ||
+ | msg = MIMEText(corps, | ||
+ | else: | ||
+ | # message " | ||
+ | msg = MIMEMultipart(' | ||
+ | |||
+ | msg[' | ||
+ | msg[' | ||
+ | msg[' | ||
+ | msg[' | ||
+ | msg[' | ||
+ | msg[' | ||
+ | msg[' | ||
+ | msg[' | ||
+ | |||
+ | if pjointes != []: | ||
+ | msg.attach(MIMEText(corps, | ||
+ | |||
+ | # ajout des pièces jointes | ||
+ | for fichier in pjointes: | ||
+ | part = MIMEBase(' | ||
+ | try: | ||
+ | with open(fichier, | ||
+ | part.set_payload(f.read()) | ||
+ | except Exception as msgerr: | ||
+ | raise ValueError (" | ||
+ | encoders.encode_base64(part) | ||
+ | part.add_header(' | ||
+ | msg.attach(part) | ||
+ | |||
+ | # compactage final du message dans les 2 cas (avec ou sans pièce(s) jointe(s)) | ||
+ | self.mail = msg.as_string() | ||
+ | |||
+ | # construction de la liste complète de tous les destinataires (to + cc + bcc) | ||
+ | self.destinataires = to | ||
+ | self.destinataires.extend(cc) | ||
+ | self.destinataires.extend(bcc) | ||
+ | |||
+ | ############################################################################## | ||
+ | def envoieSMTP(message, | ||
+ | """ | ||
+ | |||
+ | # | ||
+ | # connexion au serveur SMTP | ||
+ | smtp = None | ||
+ | try: | ||
+ | smtp = SMTP(serveur.adresse, | ||
+ | except Exception as msgerr: | ||
+ | if smtp != None: | ||
+ | smtp.quit() | ||
+ | return " | ||
+ | |||
+ | # | ||
+ | # à décommenter pour avoir tous les échanges du protocole dans la console | ||
+ | # smtp.set_debuglevel(1) | ||
+ | |||
+ | # | ||
+ | # ouverture de session | ||
+ | if serveur.login != "": | ||
+ | try: | ||
+ | rep = smtp.login(serveur.login, | ||
+ | except Exception as msgerr: | ||
+ | if smtp != None: | ||
+ | smtp.quit() | ||
+ | return " | ||
+ | |||
+ | # | ||
+ | # envoi du mail | ||
+ | try: | ||
+ | rep = smtp.sendmail(message.expediteur, | ||
+ | except Exception as msgerr: | ||
+ | if smtp != None: | ||
+ | smtp.quit() | ||
+ | return " | ||
+ | |||
+ | # | ||
+ | # ici, l' | ||
+ | smtp.quit() | ||
+ | return "" | ||
+ | |||
+ | </ | ||
+ | |||
===== Envoi d'un mail " | ===== Envoi d'un mail " |