python3 邮件,多用户,抄送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #!/usr/bin/env python # -*- coding: utf- 8 -*- # @Time : 2017 / 8 / 19 10 : 44 # @Author : Lys # @Site : # @File : mail.py # @Software : PyCharm Community Edition import sys import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication class Mailer(object): def __init__(self, maillist,mailcclist, mailtitle, mailcontent): self.mail_list = maillist self.mail_cc = mailcclist self.mail_title = mailtitle self.mail_content = mailcontent self.mail_host = "xxx.xxx.com" #邮件服务器地址 self.mail_user = "xxx@xxx.com" #USER self.mail_pass = "xxxxx" #Password #self.mail_postfix = "xxxxxx.com" def sendMail(self): me = self.mail_user + "<" + self.mail_user + "@" + ">" msg = MIMEMultipart() msg[ 'Subject' ] = 'Python mail Test' msg[ 'From' ] = me msg[ 'To' ] = ";" .join(self.mail_list) msg[ 'Cc' ] = ";" .join(self.mail_cc) #print( "++++++++++++++" ,self.mail_cc) # puretext = MIMEText( '<h1>你好,<br/>' +self.mail_content+ '</h1>' , 'html' , 'utf-8' ) puretext = MIMEText( '纯文本内容' + self.mail_content) msg.attach(puretext) #txt类型格式 txtpart = MIMEApplication(open( "E:\day_2\s1\hello.txt" , 'rb' ).read()) txtpart.add_header( 'Content-Disposition' , 'attachment' , filename= 'hello.txt' ) msg.attach(txtpart) # jpg类型的附件 #jpgpart = MIMEApplication(open( '/home/mypan/1949777163775279642.jpg' , 'rb' ).read()) #jpgpart.add_header( 'Content-Disposition' , 'attachment' , filename= 'beauty.jpg' ) #msg.attach(jpgpart) # 首先是xlsx类型的附件 # xlsxpart = MIMEApplication(open( 'test.xlsx' , 'rb' ).read()) # xlsxpart.add_header( 'Content-Disposition' , 'attachment' , filename= 'test.xlsx' ) # msg.attach(xlsxpart) # mp3类型的附件 # mp3part = MIMEApplication(open( 'kenny.mp3' , 'rb' ).read()) # mp3part.add_header( 'Content-Disposition' , 'attachment' , filename= 'benny.mp3' ) # msg.attach(mp3part) # pdf类型附件 # part = MIMEApplication(open( 'foo.pdf' , 'rb' ).read()) # part.add_header( 'Content-Disposition' , 'attachment' , filename= "foo.pdf" ) # msg.attach(part) try : s = smtplib.SMTP() # 创建邮件服务器对象 s.connect(self.mail_host) # 连接到指定的smtp服务器。参数分别表示smpt主机和端口 s.login(self.mail_user, self.mail_pass) # 登录到你邮箱 #print( "-------" ,self.mail_list,self.mail_cc) s.sendmail(me,self.mail_list + self.mail_cc,msg.as_string()) # 发送内容 s.close() return True except Exception as e: print(str(e)) return False def main(mailto_list,mailcc_list): mailto_list = mailto_list mailcc_list = mailcc_list mail_title = 'Hey subject' mail_content = 'Hey this is content' mm = Mailer(mailto_list, mailcc_list, mail_title, mail_content) res = mm.sendMail() print(res) #将消息类型转换为list def processMsg(people_list): mail_list = [] while True: try : get_mail_index = str(people_list).index( ',' ) get_first = (people_list[:get_mail_index]) mail_list.append(get_first) people_list = people_list[get_mail_index+ 1 :] except ValueError: mail_list.append(people_list) break return mail_list if __name__ == '__main__' : send_list = sys.argv[ 1 ] cc_list = sys.argv[ 2 ] mailto_list = [] mailcc_list = [] mailto_list = processMsg(send_list) mailcc_list = processMsg(cc_list) main(mailto_list,mailcc_list) #main([ "xxx@xxx.com" , "xxx@xxx.com" ], [ 'xxxx@qq.com' , "xxx@xxx.com" ]) |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 2025成都.NET开发者Connect圆满结束
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络