自动化任务完成后需要自动发送邮件记录结果,发送邮件需要用到smtplib模块,直接使用import导入即可:
163邮箱作为发件箱:在邮箱设置里面打开pop3/smtp,设置授权码
1 import smtplib 2 from email.mime.text import MIMEText 3 username=‘[email protected]‘ #发送者账号 4 passwd=‘xxxxxx‘#必须是授权码 5 msg=MIMEText(‘新年好!‘)#邮件内容 6 msg[‘Subject‘]=‘昔年快乐‘ #邮件标题 7 msg[‘From‘]=username 8 msg[‘To‘]=‘[email protected]‘ #发送到邮箱 9 msg[‘Cc‘]=‘[email protected]‘ #抄送人邮箱 10 smtp=smtplib.SMTP(‘smtp.163.com‘,port=25)#连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25 11 smtp.login(username,passwd)# 发送者的邮箱账号,密码 12 smtp.sendmail(username,‘[email protected]‘,msg.as_string())# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串 13 smtp.quit()# 发送完毕后退出smtp 14 print(‘发送成功‘)
qq邮箱作为发件箱:
1 import smtplib 2 from email.mime.text import MIMEText 3 username=‘[email protected]‘ 4 passwd=‘kpkvjjwlxqxgbcjd‘#必须是授权码 5 msg=MIMEText(‘新年好!‘) 6 msg[‘Subject‘]=‘昔年快乐‘ 7 msg[‘From‘]=username 8 msg[‘To‘]=‘[email protected]‘ 9 msg[‘Cc‘]=‘[email protected]‘ 10 smtp=smtplib.SMTP_SSL(‘smtp.qq.com‘,port=465) 11 smtp.login(username,passwd) 12 smtp.sendmail(username,‘[email protected]‘,msg.as_string()) 13 smtp.quit() 14 print(‘发送成功‘)
添加附件后发送邮件:
1 1 import smtplib 2 2 from email.mime.text import MIMEText 3 3 from email.mime.multipart import MIMEMultipart 4 4 username=‘[email protected]‘# 发送者账号 5 5 email_host = ‘smtp.163.com‘#邮箱地址 6 6 passwd=‘xxxxx‘#163邮箱的授权码 7 7 recv=‘[email protected]‘#收件人邮箱,多个账号的话,用逗号隔开 8 8 title=‘邮件标题‘ 9 9 content=‘发送邮件测试‘ 10 10 msg = MIMEMultipart() 11 11 file=‘url编码.py‘#附件的文件名 12 12 att = MIMEText(open(file,encoding=‘utf-8‘).read())#构建一个附件的U对象 13 13 att["Content-Type"] = ‘application/octet-stream‘ 14 14 att["Content-Disposition"] = ‘attachment; filename="%s"‘%file#这个是在邮件中显示的附件名 15 15 msg.attach(att)#把刚才创建好的附件,加到正文里面 16 16 msg.attach(MIMEText(‘邮件的内容。。。。‘))#邮件正文的内容 17 17 msg[‘Subject‘] = title # 邮件主题 18 18 msg[‘From‘] = username # 发送者账号 19 19 msg[‘To‘] = recv # 接收者账号列表 20 20 # smtp = smtplib.SMTP_SSL(email_host,port=456)#qq邮箱 21 21 smtp = smtplib.SMTP(email_host,port=25)#其他邮箱 22 22 smtp.login(username,passwd) 23 23 smtp.sendmail(username,recv,msg.as_string()) 24 24 smtp.quit()
封装成类,使用的时候直接调用函数,传入邮箱账号密码,收件人,发件人,标题和内容即可:
1 import smtplib,os 2 from email.mime.text import MIMEText 3 from email.mime.multipart import MIMEMultipart 4 import base64 5 class SendMail(object): 6 def __init__(self,username,passwd,recv,title,content,email_host, 7 file=None,ssl=False, 8 port=25,ssl_port=465): 9 ‘‘‘ 10 :param username: 用户名 11 :param passwd: 密码 12 :param recv: 收件人,多个要传list [‘[email protected]‘,‘[email protected]] 13 :param title: 邮件标题 14 :param content: 邮件正文 15 :param file: 附件路径,如果不在当前目录下,要写绝对路径,默认没有附件 16 :param ssl: 是否安全链接,默认为普通 17 :param email_host: smtp服务器地址,默认为163服务器 18 :param port: 非安全链接端口,默认为25 19 :param ssl_port: 安全链接端口,默认为465 20 ‘‘‘ 21 self.username = username #用户名 22 self.passwd = passwd #密码 23 self.recv = recv #收件人,多个要传list [‘[email protected]‘,‘[email protected]] 24 self.title = title #邮件标题 25 self.content = content #邮件正文 26 self.file = file #附件路径,如果不在当前目录下,要写绝对路径 27 self.email_host = email_host #smtp服务器地址 28 self.port = port #普通端口 29 self.ssl = ssl #是否安全链接 30 self.ssl_port = ssl_port #安全链接端口 31 def send_mail(self): 32 msg = MIMEMultipart() 33 #发送内容的对象 34 if self.file:#处理附件的 35 file_name = os.path.split(self.file)[-1]#只取文件名,不取路径 36 try: 37 f = open(self.file, ‘rb‘).read() 38 except Exception as e: 39 raise Exception(‘附件打不开!!!!‘) 40 else: 41 att = MIMEText(f,"base64", "utf-8") 42 att["Content-Type"] = ‘application/octet-stream‘ 43 #base64.b64encode(file_name.encode()).decode() 44 new_file_name=‘=?utf-8?b?‘ + base64.b64encode(file_name.encode()).decode() + ‘?=‘ 45 #这里是处理文件名为中文名的,必须这么写 46 att["Content-Disposition"] = ‘attachment; filename="%s"‘%(new_file_name) 47 msg.attach(att) 48 msg.attach(MIMEText(self.content))#邮件正文的内容 49 msg[‘Subject‘] = self.title # 邮件主题 50 msg[‘From‘] = self.username # 发送者账号 51 msg[‘To‘] = ‘,‘.join(self.recv) # 接收者账号列表 52 if self.ssl: 53 self.smtp = smtplib.SMTP_SSL(self.email_host,port=self.ssl_port) 54 else: 55 self.smtp = smtplib.SMTP(self.email_host,port=self.port) 56 #发送邮件服务器的对象 57 self.smtp.login(self.username,self.passwd) 58 try: 59 self.smtp.sendmail(self.username,self.recv,msg.as_string()) 60 pass 61 except Exception as e: 62 print(‘出错了。。‘,e) 63 else: 64 print(‘发送成功!‘) 65 self.smtp.quit() 66 67 68 if __name__ == ‘__main__‘: 69 m = SendMail( 70 username=‘[email protected]‘, 71 passwd=‘cjkkvbbcdfrabejf‘, 72 recv=[‘[email protected]‘], 73 title=‘邮件标题‘, 74 content=‘祝aa新年快乐,万事如意,心想事成,狗年大吉!‘, 75 email_host=‘smtp.qq.com‘, 76 file=r‘C:\Users\DELL\Desktop\邮件附件.txt‘,ssl=True, 77 ) 78 m.send_mail()