python gmail 多个收件人
时间:2011-01-17 来源:遥望
#!send gmail with python
import smtplib,email,os,sys
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email import Encoders
from email.header import Header
fromaddr = '####@gmail.com' #where the mail from
toaddrs = ['***@126.com','****@gmail.com','****@hotmail.com']
to_string =''
for item in toaddrs:
to_string += item +','
subject = "test##############"
msg = MIMEMultipart()
#msg = MIMEText('body')
attachment = '附件文件名'
#msg = "\nhell"
print toaddrs
# Credentials (if needed)
username = '####'
password = 'pass'
msg["From"] = fromaddr
msg["To"] = to_string
msg["Subject"] = subject
body = "i want to send multi mail with python2222 MIME!"
msg.attach(MIMEText(body))
#attachment
fp = open(attachment,"rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(fp.read())
fp.close()
Encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment; filename=%s" % attachment)
msg.attach(part)
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg.as_string())
print 'success!'
server.quit()