我使用以下方法,效果很好:
1)将密码存储在单独的gpg加密文件中。例如~/.passwd/<accountname>.gpg
2)使用您选择的名称(例如)创建一个python扩展文件,其~/.offlineimap.py
内容如下:
def mailpasswd(acct):
acct = os.path.basename(acct)
path = "/home/<username>/.passwd/%s.gpg" % acct
args = ["gpg", "--use-agent", "--quiet", "--batch", "-d", path]
try:
return subprocess.check_output(args).strip()
except subprocess.CalledProcessError:
return ""
3)修改您的.offlineimaprc文件,以告知其有关python文件的信息,并告诉其如何读取密码
[general]
pythonfile = ~/.offlineimap.py
# ...
[Repository <reponame>]
# add this line for each remote repository
remotepasseval = mailpasswd("<accountname>")
如果您有多个同时被检查的帐户(单独的线程),并且您使用gpg-agent,则它将要求您为每个帐户输入密码。我通过创建文件(echo "prime" | gpg -e -r foo@bar.com > ~/.passwd/prime.gpg
)来启动代理,并通过在offlineimap启动时解密该文件来启动gpg代理。为此,请将以下内容添加到~/.offlineimap.py
:
def prime_gpg_agent():
ret = False
i = 1
while not ret:
ret = (mailpasswd("prime") == "prime")
if i > 2:
from offlineimap.ui import getglobalui
sys.stderr.write("Error reading in passwords. Terminating.\n")
getglobalui().terminate()
i += 1
return ret
prime_gpg_agent()