home changes contents help options

404:SMTPメールを送る

こちらの解説とサンプルが一番わかりやすい。

Pythonでメールを送信したい人のためのサンプル集

この例は題目の処理を行っていないようなので少し怪しい感じがする。

http://www.python.jp/doc/nightly/lib/SMTP-example.html

こちらにあった例。リンクの張り方がわからないので、コピーさせていただいた。動作は未確認。

http://www.bigbold.com/snippets/tag/python/2

python : send a mail (text), with attachments

 import smtplib
 from email.MIMEMultipart import MIMEMultipart
 from email.MIMEBase import MIMEBase
 from email.MIMEText import MIMEText
 from email.Utils import COMMASPACE, formatdate
 from email import Encoders
 import os

 def sendMail(to, subject, text, files=[],server="localhost"):
     assert type(to)==list
     assert type(files)==list
     fro = "Expediteur <expediteur@mail.com>"

     msg = MIMEMultipart()
     msg['From'] = fro
     msg['To'] = COMMASPACE.join(to)
     msg['Date'] = formatdate(localtime=True)
     msg['Subject'] = subject

     msg.attach( MIMEText(text) )

     for file in files:
         part = MIMEBase('application', "octet-stream")
         part.set_payload( open(file,"rb").read() )
         Encoders.encode_base64(part)
         part.add_header('Content-Disposition', 'attachment; filename="%s"'
                        % os.path.basename(file))
         msg.attach(part)

     smtp = smtplib.SMTP(server)
     smtp.sendmail(fro, to, msg.as_string() )
     smtp.close() 

 sendMail(
         ["destination@dest.kio"],
         "hello","cheers",
         ["photo.jpg","memo.sxw"]
     )