您的位置:首页 > 编程语言 > Python开发

python实现的邮件自动群发脚本

2011-03-31 21:14 791 查看
很多时候我们可能需要在某种情况下自动发送邮件给对方,标准库的smtplib可以实现这个功能,代码比较简单。

#!/usr/bin/env python

import smtplib
from email.mime.text import MIMEText

#list of mail address you wana to send
to = ["xx@xx", "xx@xx"]

#user and password of your mail
MailHost = "smtp.163.com"
MailUser = "xxxxxx"
MailPswd = "******"
MailPostfix = "163.com"

def SendMail(to, sub, content):

Me = MailUser + "<" + MailUser + "@" + MailPostfix + ">"
Msg = MIMEText(content)
Msg['Subject'] = sub
Msg['From'] = Me
Msg['To'] = ";".join(to)

try:
s = smtplib.SMTP()
s.connect(MailHost)
s.login(MailUser, MailPswd)
s.sendmail(Me, to, Msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False

def main():

sub = "Hello"
content = "This is a test mail"
if SendMail(to, sub, content):
print 'send successful'
else:
print 'send failed'
return 0

if __name__ == '__main__':
main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: