近来有些东西需要监控报警发邮件,然后在网上找了点材料,自己写了一个简单发送邮件的脚本,主要就是运用python的smtplib模块,分享给大家看一下:
复制代码 代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#导入smtplib和mimetext
import smtplib,sys
from email.mime.text import mimetext
def send_mail(sub,content):
#############
#要发给谁,这里发给1个人
mailto_list=[wangwei03@jb51.net]
#####################
#设置服务器,用户名、口令以及邮箱的后缀
mail_host=mail.gyyx.cn
mail_user=wangwei03@jb51.net
mail_pass=123456677890
mail_postfix=gyyx.cn
######################
'''''
to_list:发给谁
sub:主题
content:内容
send_mail(aaa@126.com,sub,content)
'''
me=mail_user+
msg = mimetext(content,_charset='gbk')
msg['subject'] = sub
msg['from'] = me
msg['to'] = ;.join(mailto_list)
try:
s = smtplib.smtp()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, mailto_list, msg.as_string())
s.close()
return true
except exception, e:
print str(e)
return false
if __name__ == '__main__':
if send_mail(u'这是python测试邮件',u'python发送邮件'):
print u'发送成功'
else:
print u'发送失败'