如何使用Python发送电子邮件而不泄露密码?

时间:2014-01-08 03:01:02

标签: python email

此代码段有效,但代码中的密码是敞开的:

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("usrname", "pw")

msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("me@gmail.com", "u@gmail.com", msg)
server.quit()

我想知道如何安全地做到这一点。

编辑:

我正在寻找“密钥存储”解决方案,例如任何真正的电子邮件客户端使用的解决方案,它可以安全地(希望)将您的密码存储在磁盘上。

2 个答案:

答案 0 :(得分:1)

查看getpass模块。您可以使用getpass来提示输入密码,以便它不会存储在代码中。

<强>用法:

>>> import getpass
>>> password = getpass.getpass()
Password: 
>>> print password
s3cr3t

答案 1 :(得分:1)

也许你可以:

使用env变量eg :: export PWD=xx,然后使用python:import os; pwd = os.environ.get('PWD')

使用文件记录密码,然后将其放在其他位置。然后with open(passwdfile) as fi: pwd=fi.read().strip()

如果你的smtp服务器支持,推荐使用ssl。