Enter password securely when sending automated email

Viewed 91

This may well be a duplicate ... but I have looked through the similar questions quite carefully. This is the kind of thing, but it's in C#. Or this, using Windows PowerShell. Or this for PHP.

I have just discovered how to send automated emails from a Python script and I intend to use these for various purposes, including when various sys admin tasks need to issue an alert.

I don't want such scripts to prompt the user for the password to the email account, but obviously I don't want to write the password out in the .py file either.

What's the right way to go about this? Is there a specific python solution to this?

2 Answers

It seems as though Django's auth module may meet this requirement. I haven't got time to look into it at the moment. I'm not clear, for example, how much of Django you have to include, i.e. whether you can just add this module in isolation.

I would suggest storing the password in a json file (assuming encryption is not a requirement). Something like this:

import json
import warnings
warnings.filterwarnings("ignore")
conf = json.load(open('secretfile.json'))
# Read arguments from json file
pass_name = conf['pass']

The secretfile.json file should look something like this:

{
    "pass" : "my_password"
}
Related