python脚本发送邮件
2017-11-02 15:10
375 查看
def SendMail(fromAddress, toAddress, usepassword,subject=None, content=None, attfile=None, \ subtype='plain', charset='utf-8'): username = fromAddress #创建一个带附件的实例 msg = MIMEMultipart() msg['From'] = fromAddress msg['To'] = toAddress if subject: #标题 msg['Subject'] = subject msg['Date'] = Utils.formatdate(localtime=1) if content: #添加邮件内容 txt = MIMEText(content, subtype, charset) msg.attach(txt) if attfile: #构造附件 #注意:传入的参数attfile为unicode,否则带中文的目录或名称的文件读不出来 # basename 为文件名称,由于传入的参数attfile为unicode编码,此处的basename也为unicode编码 basename = os.path.basename(attfile) print basename #注意:指定att的编码方式为gb2312 att = MIMEText(open(attfile, 'rb').read(), 'base64', 'gb2312') att["Content-Type"] = 'application/octet-stream' #注意:此处basename要转换为gb2312编码,否则中文会有乱码。 # 特别,此处的basename为unicode编码,所以可以用basename.encode('gb2312') # 如果basename为utf-8编码,要用basename.decode('utf-8').encode('gb2312') att["Content-Disposition"] = 'attachment; filename=%s' % basename.encode('gb2312') msg.attach(att) try: #smtp = smtplib smtp = smtplib.SMTP() #连接服务器 smtp.connect('smtp.163.com', '25') #登录 smtp.login(username, usepassword) #发送邮件 smtp.sendmail(fromAddress, toAddress, msg.as_string()) #退出 smtp.quit() print('邮件发送成功email has send out !') #调用 if __name__ == "__main__": #注意:附件的路径字符串应为unicode编码 SendMail('xxxxxxx', 'xxxxxxxx', 'xxxxxxx','编译结果','编译log文件已发送,请查看!',u'编译结果.h')
相关文章推荐
- 监控redis和zookpeer服务脚本 并且python发送邮件
- Python自动发送邮件的脚本源码
- Python实现的简单发送邮件脚本分享
- python发送邮件的脚本
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送
- python邮件发送脚本
- zabbix 发送邮件脚本(python shell)
- 使用python脚本发送邮件
- Python绘制excel表格到邮件正文区并发送脚本实例
- 系统访问日志统计及邮件发送脚本--python实现
- python脚本获取zabbix监控数据,并且通过邮件发送
- Tomcat错误日志发送邮件(python)脚本
- python脚本发送普通邮件
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件
- postfix python发送邮件脚本配置(不乱码)
- python 脚本发送邮件
- zabbix增加手机短信、邮件监控的注意要点,SSL邮件发送python脚本
- Python实现的简单发送邮件脚本分享
- 终于用PYTHON作了一个实用于工作的脚本啦----通过邮件定时发送服务器空闲空间
- Python脚本实现发送邮件功能