1、通过Python的 yagmail模块发送邮件的前提:
#!/usr/bin/env python # -*- coding: utf-8 -*- #author tom import yagmail args={ "user":"302624476@qq.com", #发邮件账号 "password":"xxxxxx", #发件账号对应的密码(QQ使用的是授权码,而不是QQ密码) "host":"smtp.qq.com", #邮件账号的SMTP服务器 "port":"465" #SMTP服务器端口 465 } # 收件人列表 emailList=["tjp1991@126.com"] #实例化发送者 email = yagmail.SMTP(**args) # 调用消息发送函数,参数分别是:to=收件人列表,subject 邮件标题,contents 邮件正文,cc 邮件抄送人 # email.send(to=emailList,subject="My name is tom",contents="Today is 20190724",cc="jsh******@sina.com") email.send(to=emailList,subject="My name is tom",contents="Today is 20190724")
实例:
#!/usr/bin/env python # -*- coding: utf-8 -*- #author tom import email.mime.multipart import email.mime.text import smtplib msg = email.mime.multipart.MIMEMultipart() #创建消息对象 msg['from'] = '302624476@qq.com' #指定发件人,这边写了,发送的还好就不用写了 msg['to'] = 'tjp1991@126.com' #指定收件人,这边写了,发送的时候就不用写了 msg['subject'] = '自动右键测试' #写明邮件主题 #定义邮件内容 context = ''' <h1>2019724-TEST</h1> ''' #_text代表邮件内容,_subtype代表邮件内容的发送形式 text = email.mime.text.MIMEText(_text=context, _subtype="html") msg.attach(text) em = smtplib.SMTP_SSL() em.connect("smtp.qq.com", 465) em.login("302624476@qq.com", 'xxx') em.sendmail(from_addr='302624476@qq.com', to_addrs='tjp1991@126.com', msg=msg.as_string()) em.quit()
封装好的发送附件功能:
#!/usr/bin/env python # -*- coding: utf-8 -*- #author tom import smtplib,os from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import base64 class SendMail(object): def __init__(self,username,passwd,recv,title,content, file=None,ssl=False, email_host='smtp.qq.com',port=25,ssl_port=465): ''' :param username: 用户名 :param passwd: 密码 :param recv: 收件人,多个要传list ['a@qq.com','b@qq.com] :param title: 邮件标题 :param content: 邮件正文 :param file: 附件路径,如果不在当前目录下,要写绝对路径,默认没有附件 :param ssl: 是否安全链接,默认为普通 :param email_host: smtp服务器地址,默认为163服务器 :param port: 非安全链接端口,默认为25 :param ssl_port: 安全链接端口,默认为465 ''' self.username = username #用户名 self.passwd = passwd #密码 self.recv = recv #收件人,多个要传list ['a@qq.com','b@qq.com] self.title = title #邮件标题 self.content = content #邮件正文 self.file = file #附件路径,如果不在当前目录下,要写绝对路径 self.email_host = email_host #smtp服务器地址 self.port = port #普通端口 self.ssl = ssl #是否安全链接 self.ssl_port = ssl_port #安全链接端口 def send_mail(self): msg = MIMEMultipart() #发送内容的对象 if self.file:#处理附件的 file_name = os.path.split(self.file)[-1]#只取文件名,不取路径 try: f = open(self.file, 'rb').read() except Exception as e: raise Exception('附件打不开!!!!') else: att = MIMEText(f,"base64", "utf-8") att["Content-Type"] = 'application/octet-stream' #base64.b64encode(file_name.encode()).decode() new_file_name='=?utf-8?b?' + base64.b64encode(file_name.encode()).decode() + '?=' #这里是处理文件名为中文名的,必须这么写 att["Content-Disposition"] = 'attachment; filename="%s"'%(new_file_name) msg.attach(att) msg.attach(MIMEText(self.content))#邮件正文的内容 msg['Subject'] = self.title # 邮件主题 msg['From'] = self.username # 发送者账号 msg['To'] = ','.join(self.recv) # 接收者账号列表 if self.ssl: self.smtp = smtplib.SMTP_SSL(self.email_host,port=self.ssl_port) else: self.smtp = smtplib.SMTP(self.email_host,port=self.port) #发送邮件服务器的对象 self.smtp.login(self.username,self.passwd) try: self.smtp.sendmail(self.username,self.recv,msg.as_string()) pass except Exception as e: print('出错了。。',e) else: print('发送成功!') self.smtp.quit() if __name__ == '__main__': m = SendMail( username='302624476@qq.com', passwd='xxxxx', recv=['tjp1991@126.com'], title='python发送邮件附件测试', content='测试发送邮件,qq发件,接收方一个是126邮箱,20190724', file=r'E:\\idm下载\\01爬虫实战-python实现app数据抓取.pptx', ssl=True, ) m.send_mail()