我们在执行input('请输入你的密码:')时候,会显示明文,而getpass就解决了明文显示的问题:
1 我们在执行input('请输入你的密码:')时候,会显示明文,而getpass就解决了明文显示的问题: 2 3 import getpass 4 import smtplib 5 from email.mime.text import MIMEText 6 7 class Sendmain(object): 8 def __init__(self,fromuser,pwd,touser,title,content,host='smtp.sina.cn',port=25): 9 self.touser=touser 10 self.content=content 11 self.title=title 12 self.host=host 13 self.port=port 14 self.fromuser=fromuser 15 self.pwd=pwd 16 17 def send_main(self): 18 msg=MIMEText(self.content,'plain','utf-8') 19 msg["Subject"]=self.title 20 msg['From']=self.fromuser 21 msg['To']=self.touser 22 23 try: 24 smtp=smtplib.SMTP(self.host,self.port) 25 smtp.login(self.fromuser,self.pwd) 26 smtp.sendmail(self.fromuser,self.touser,msg.as_string()) 27 smtp.quit() 28 print('邮件发送成功') 29 30 except smtplib.SMTPException as e: 31 print(e) 32 33 if __name__ == '__main__': 34 fromuser=input('请输入发送人账号:') 35 pwd=getpass.getpass("输入你的密码:") 36 touser='[email protected]' 37 title='密码练习' 38 content='getpass练习' 39 send=Sendmain(fromuser,pwd,touser,title,content) 40 send.send_main()
执行结果入图:
要注意的是:在执行的时候,只能在终端里进行,否则getpass无效