自动发送邮件工具-Auto Send Mail Tool (ASM)

来源:互联网 发布:什么叫不支持网络打印 编辑:程序博客网 时间:2024/05/20 01:09

joyyzhang将源码已经放在了github上
https://github.com/zydirtyfish/asm.git
欢迎Fork!!!

joyyzhang为了能够实时监测服务器上程序的运行状态实现的一个自动发送email的程序。

首先看一下README.md
asm
Auto Send Mail Tool ( ASM )

This tool can help you to send emails in the form of HTML automatically. The only thing you need to do is to write the mail_content according to the format given in the source file. You can also modify this file by using the api given in writeAPI.py.

Start your “Hello world” mail by excuting sh auto_send.sh if you have create the mail_content metioned above.

send_mail.py
This is the kernel file by encapsulating smtplib. There is no need for you to know how it is realized, because you only need to modify the mail_content if you want to send mails automatically.

writeAPI.py
You can modify the mail_content much easier by using the api in this file.
- write_content(html) : To modify the mail_msg
- write_send_list(send_list) : To modify the send_list
- write_accessory_list(accessory_list) : To modify the accessory list
- write_img_list(img_list) : To modify the images contained in the main_msg
- write_all(content,subject,encoding,send_list,accessory_list,img_list) : To modify the parameters all above

mail_config
Always likes this.

[mail_config]sender = XXXX@yyy.com It is your own email addresssender_name = your_name Your own namepassword = **** Password of you email, always the smtp passwordmail_host = smtp.yyy.com SMTP server domain. You can always find it on the Internetmail_postfix = yyy.com

mail_content
Always likes this.

[mail_content]send_list = XXXX@yyy.com who will receive this mailsubject = Pic Subject of this mailencoding = utf-8 Encoding of the main contentaccessory_list = | Accessories contained in the mailmail_msg = *** main_content of the mail, always the html img_list = .jpg|.jpg images contained in the mail_msg

核心代码段如下

send_mail.py

#!/usr/bin/python# -*- coding: UTF-8 -*-import smtplibfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.header import Headerimport ConfigParserimport string,os,sysdef send_list():    try:        cf = ConfigParser.ConfigParser()        cf.read("mail_content")        send_str=cf.get("mail_content","send_list")        return send_str.split("|")    except:        print "请在mail_content文件的[mail_content]元素下添加sendlist项"        exit()def get_config():    cf = ConfigParser.ConfigParser()    try:        cf.read("mail_config")        mail_config={}        mail_config["sender"]=cf.get("mail_config","sender")        mail_config["sender_name"]=cf.get("mail_config","sender_name")        mail_config["password"]=cf.get("mail_config","password")        mail_config["mail_host"]=cf.get("mail_config","mail_host")        mail_config["mail_postfix"]=cf.get("mail_config","mail_postfix")    except:        print "请给出完整的mail_config文件,否则无法完成邮件发送,mail_config示例在源码中含有"        exit()    return mail_configdef get_accessory_list():    cf =ConfigParser.ConfigParser()    try:        cf.read("mail_content")        accessory_list = cf.get("mail_content","accessory_list")        if accessory_list != "":             return accessory_list.split("|")        else:            return []    except:        return []def get_content():    mail_content = {}    cf = ConfigParser.ConfigParser()    try:        cf.read("mail_content")    except:        print "请给出完整的mail_content文件,否则无法完成邮件发送,mail_content示例在源码中含有"        exit()    try:        mail_content["subject"]=cf.get("mail_content","subject")    except:        mail_content["subject"]="NO TITLE"    try:                mail_content["encoding"]=cf.get("mail_content","encoding")    except:        mail_content["encoding"]="utf-8"    try:        mail_content["mail_msg"]=cf.get("mail_content","mail_msg")    except:        mail_content["mail_msg"]=""    try:        img_list = cf.get("mail_content","img_list")        mail_content["img_list"]=[]        if img_list != "":            mail_content["img_list"]=img_list.split("|")    except:        mail_content["img_list"]=[]    return mail_contentdef send_mail_function(mail_config,content,send_list,accessory_list):    #设置SMTP服务器、发送者、口令    sender = mail_config["sender"]    password = mail_config["password"]    mail_host= mail_config["mail_host"]     #设置接受者列表    receivers = send_list  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱    msgRoot = MIMEMultipart('related')    msgRoot['From'] = mail_config["sender_name"]+"<"+sender+">"    #msgRoot['To'] =  ""    msgRoot['Subject'] = Header(content["subject"], content["encoding"])    msgAlternative = MIMEMultipart('alternative')    msgRoot.attach(msgAlternative)    msgAlternative.attach(MIMEText(content["mail_msg"], 'html', 'utf-8'))    i = 1;    for img in content["img_list"]:        # 指定图片为当前目录        fp = open(img, 'rb')        msgImage = MIMEImage(fp.read())        fp.close()        # 定义图片 ID,在 HTML 文本中引用        msgImage.add_header('Content-ID', '<image%d>'%i)        msgRoot.attach(msgImage)        i=i+1    for accessory in accessory_list :         fp = open(accessory, 'rb')        accessory_file = MIMEImage(fp.read())        fp.close()        msgRoot.attach(accessory_file)    try:        smtpObj = smtplib.SMTP()        smtpObj.connect(mail_host)        smtpObj.login(sender,password)        smtpObj.sendmail(sender, receivers, msgRoot.as_string())        print "Success!!!"    except smtplib.SMTPException,e:        print e            print "Error: Fail To Send"    smtpObj.close()def main():    #print get_content()    send_mail_function(get_config(),get_content(),send_list(),get_accessory_list())if __name__ == "__main__":    main()

[注意]使用ASM是必须先开启邮箱的SMTP服务,此时会有一个SMTP验证密码,上面配置文件中的密码需要填写此密码,而不是你的邮箱密码。

0 0