2

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?

mike rodent
  • 14,126
  • 11
  • 103
  • 157

2 Answers2

0

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.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
-1

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"
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Stein
  • 719
  • 7
  • 9
  • Thanks. I'm wondering whether or how that file secretfile.json can be kept as secret as possible? It's just that I wouldn't keep an email account password in an unencrypted file on my Linux or W10 partition in the normal run of things (although these partitions are themselves password-protected of course). Adding something about linked Windows PowerShell question on these line... – mike rodent Oct 18 '19 at 07:49
  • @mikerodent maybe encrypt the file with a password? – Legorooj Oct 18 '19 at 07:55
  • @Legorooj Circular solution! The password to that encryption would have to be hard-coded in the .py file or use another (unencrypted) .json file. Check out the links I've put to C# and PowerShell solutions to this. – mike rodent Oct 18 '19 at 07:59