• python 发送邮件附件及文字信息


     一,发送附件
    1
    #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 def smtp(file): 4 from email.mime.text import MIMEText 5 from email.mime.multipart import MIMEMultipart 6 import smtplib 7 8 msg=MIMEMultipart() 9 10 #构造附件1 11 att1 = MIMEText(open(file, 'rb').read(), 'base64',_charset='utf-8') 12 att1["Content-Type"] = 'application/octet-stream' 13 att1["Content-Disposition"] = 'attachment; filename='+file#这里的filename可以任意写,写什么名字,邮件中显示什么名字 14 msg.attach(att1) 15 16 #构造附件2 17 #att2 = MIMEText(open('d:\123.txt', 'rb').read(), 'base64', 'gb2312') 18 #att2["Content-Type"] = 'application/octet-stream' 19 #att2["Content-Disposition"] = 'attachment; filename="123.txt"' 20 #msg.attach(att2) 21 22 #加邮件头 23 #msg=MIMEText(file,_charset='utf-8') 24 #to_list=['jij@wangdaizhijia.com','taoj@wangdaizhijia.com','fanjj@wangdaizhijia.com'] 25 26 #能过check.py文件中传来的值判断所要发送的文件,然后然用户名添加到to_list中 27 if file == "/python/ipproxy_all.txt": 28 to_list=['cheng@test.com','liush@test.com'] 29 #if file == "/python/ipproxy_gl.txt": 30 # to_list=['liush@wangdaizhijia.com'] 31 32 #msg['to'] = 'wdzj_script@wangdaizhijia.com' 33 #msg['two'] = 'jij@wangdaizhijia.com' 34 #msg['three'] = 'taoj@wangdaizhijia.com' 35 #msg['four'] = 'fanjj@wangdaizhijia.com' 36 37 msg['from'] = 'wdzj_script@test.com' 38 msg['subject'] = 'ipzdaye proxy ipaddress!' 39 #发送邮件 40 try: 41 server = smtplib.SMTP() 42 #server.connect('smtp.163.com') 43 server.connect('smtp.exmail.qq.com') 44 server.login("wdzj_script@test","test@2014")#XXX为用户名,XXXXX为密码 45 server.sendmail(msg['from'],to_list,msg.as_string()) 46 server.quit() 47 print '发送成功' 48 except Exception, e: 49 print str(e) 50 if __name__ == '__main__': 51 smtp('test') 52 53 54
    二,发送文字信息 56 #!/usr/bin/python 57 # -*- coding: utf-8 -*- 58 def smtp(file): 59 from email.mime.text import MIMEText 60 from email.mime.multipart import MIMEMultipart 61 import smtplib 62 63 64 #加邮件头 65 msg=MIMEText(file,_charset='utf-8') 66 to_list=['liuhb@test.com','zenghui@test.com','fanlb@test.com'] 67 msg['from'] = 'wdzj_script@test.com' 68 msg['subject'] = 'webservice' 69 #发送邮件 70 try: 71 server = smtplib.SMTP() 72 server.connect('smtp.exmail.qq.com') 73 server.login('wdzj_script@test.com','test2014')#XXX为用户名,XXXXX为密码 74 server.sendmail(msg['from'],to_list,msg.as_string()) 75 server.quit() 76 print '发送成功' 77 except Exception, e: 78 print str(e) 79 if __name__ == '__main__': 80 smtp('test')
  • 相关阅读:
    Sails v1.0使用指南
    sails0.12相关命令
    vs code 快捷键
    nodejs相关框架
    sails 相关软件下载地址及命令
    UltraISO安装windows10时0x8007000D错误解决办法
    C# HtmlElement的GetAttribute("class") return ""
    Android如何实现TCP和UDP传输
    android 获取view在屏幕中的位置
    八款Android 开发者必备的小工具
  • 原文地址:https://www.cnblogs.com/zenghui940/p/4233095.html
Copyright © 2020-2023  润新知