snippets / Fufu / 

All Fufu's snippets (2)

  1. Envoyer un mail html avec python

    Comment envoyer un mail au format html en python. Pratique pour de gros envois de mail.

     1 import smtplib
    2 import MimeWriter
    3 import mimetools
    4 import StringIO
    5
    6 def sendHtmlMail(to,frommail,text,html):
    7 encoding = "base64"
    8 charset = "iso-8859-15"
    9
    10 #déclaration des buffers
    11 out = StringIO.StringIO()
    12 htmlin = StringIO.StringIO(html)
    13 txtin = StringIO.StringIO(html)
    14
    15 #déclaration et initialisation du writer
    16 writer = MimeWriter.MimeWriter(out)
    17 writer.addheader("Subject", text)
    18 writer.addheader("MIME-Version", "1.0")
    19 writer.addheader("From", frommail)
    20 writer.addheader("To", to)
    21 writer.startmultipartbody("alternative")
    22 writer.flushheaders()
    23
    24 #ajout de la partie text
    25 textPart = writer.nextpart()
    26 textPart.addheader("Content-Transfer-Encoding", encoding)
    27 pout = textPart.startbody("text/plain", [("charset", charset)])
    28 mimetools.encode(txtin, pout, encoding)
    29 txtin.close()
    30
    31 #On ajoute la partie html
    32 htmlPart = writer.nextpart()
    33 htmlPart.addheader("Content-Transfer-Encoding", encoding)
    34 pout = htmlPart.startbody("text/html", [("charset", charset)])
    35 mimetools.encode(htmlin, pout, encoding)
    36 htmlin.close()
    37
    38 #on clot le mail
    39 writer.lastpart()
    40 mail = out.getvalue()
    41 out.close()
    42 smtp = smtplib.SMTP("localhost")
    43 #smtp.connect()
    44 smtp.sendmail(frommail, [to], mail)
    45 smtp.close()
    first posted by Fufu to python python mail ... saved by 7 persons ... 0 comments ... 1 year, 2 months
  2. Création d'un mail html en python

    Cette classe permet d'envoyer un mail au format HTML en python. Très pratique pour de l'encoi de mail en masse.

     1 CLASSE MAIL.PY
    2
    3 import smtplib
    4 import MimeWriter
    5 import mimetools
    6 import StringIO
    7
    8 def sendHtmlMail(to,frommail,text,html):
    9 encoding = "base64"
    10 charset = "iso-8859-15"
    11
    12 #déclaration des buffers
    13 out = StringIO.StringIO()
    14 htmlin = StringIO.StringIO(html)
    15 txtin = StringIO.StringIO(html)
    16
    17 #déclaration et initialisation du writer
    18 writer = MimeWriter.MimeWriter(out)
    19 writer.addheader("Subject", text)
    20 writer.addheader("MIME-Version", "1.0")
    21 writer.addheader("From", frommail)
    22 writer.addheader("To", to)
    23 writer.startmultipartbody("alternative")
    24 writer.flushheaders()
    25
    26 #ajout de la partie text
    27 textPart = writer.nextpart()
    28 textPart.addheader("Content-Transfer-Encoding", encoding)
    29 pout = textPart.startbody("text/plain", [("charset", charset)])
    30 mimetools.encode(txtin, pout, encoding)
    31 txtin.close()
    32
    33 #On ajoute la partie html
    34 htmlPart = writer.nextpart()
    35 htmlPart.addheader("Content-Transfer-Encoding", encoding)
    36 pout = htmlPart.startbody("text/html", [("charset", charset)])
    37 mimetools.encode(htmlin, pout, encoding)
    38 htmlin.close()
    39
    40 #on clot le mail
    41 writer.lastpart()
    42 mail = out.getvalue()
    43 out.close()
    44 smtp = smtplib.SMTP("localhost")
    45 #smtp.connect()
    46 smtp.sendmail(frommail, [to], mail)
    47 smtp.close()
    first posted by Fufu to python python mail html ... saved by 2 persons ... 0 comments ... 1 year, 2 months
showing 10, 25, 50 items per pages

Pages : 1