我们在执行input('请输入你的密码:')时候,会显示明文,而getpass就解决了明文显示的问题:
import getpass
import smtplib
from email.mime.text import MIMEText
class Sendmain(object):
def __init__(self,fromuser,pwd,touser,title,content,host='smtp.sina.cn',port=25):
self.touser=touser
self.content=content
self.title=title
self.host=host
self.port=port
self.fromuser=fromuser
self.pwd=pwd
def send_main(self):
msg=MIMEText(self.content,'plain','utf-8')
msg["Subject"]=self.title
msg['From']=self.fromuser
msg['To']=self.touser
try:
smtp=smtplib.SMTP(self.host,self.port)
smtp.login(self.fromuser,self.pwd)
smtp.sendmail(self.fromuser,self.touser,msg.as_string())
smtp.quit()
print('邮件发送成功')
except smtplib.SMTPException as e:
print(e)
if __name__ == '__main__':
fromuser=input('请输入发送人账号:')
pwd=getpass.getpass("输入你的密码:")
touser='xxxxxxxxx@qq.com'
title='密码练习'
content='getpass练习'
send=Sendmain(fromuser,pwd,touser,title,content)
send.send_main()
执行结果入图:
![](https://img.haomeiwen.com/i17522945/e3ed8c6b763eec60.png)
网友评论