python gmail 多个收件人

用python的smtplib模块设置gmail的账号信息,给多个人发送时只需要如下的例子:

#!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()


 

posted @ 2011-01-17 13:38  随梦而飞  阅读(2219)  评论(0编辑  收藏  举报