Recently in the study of "Flask Web development: Python-based Web application development," a book in the Mail chapter, in the configuration encountered a problem, after consulting the data to find a solution. In this summary, if you are in the study of similar doubts and hope to provide you with a little help.
This paper introduces a configuration method based on environment variables, which can be saved by the environment variables by using some sensitive data information (such as mailbox user name and password), and then used by the OS module for tuning. This configuration method is often used in open source projects to ensure the security of information and facilitate system management.
Specific configuration methods in the book
① use Bash to set environment variables in Linux or Mac OS X:
(venv) $ export Mail_username=<mail username>
(venv) $ export Mail_password=<mail password>
② setting environment variables using command commands in Windows:
(venv) $ set Mail_username=<mail username>
(venv) $ set Mail_password=<mail password>
Because I am using a Windows system, this is a demonstration of the example:
It is important to note that the value of the configured property is not required to add the angle brackets <> and quotation marks, directly assigned to the value, such as set [email protected]
In this way, according to the method in the book is configured, but we found in the actual test send e-mail script does not have any effect, after debugging that the environment variable is not successfully configured, through the OS is the value of the none, and this is why the error
#test Script (send_test.py) fromFlask_mailImportMessage fromHelloImport*msg= Message ('Test Subject', sender=app.config['Mail_username'],recipients=[' Other@email. com']) Msg.body='Text Body'msg.html='<b>HTML</b> Body'With App.app_context (): Mail.send (msg)#View environment variable valuesPrint(Os.environ.get ('Mail_username'))#NonePrint(Os.environ.get ('Mail_password'))#None
Then why is this? This has to go back to command. In fact, there are two kinds of commands for setting environment variables, one is set and the other is setx. These two are similar in size, and their functional differences are small, but very critical.
Set command sets environment variables
The environment variable set by set is also called temporary environment variable, that is, its environment variable is set to expire only in the body of this window, in another form or program is naturally invalid
Basic syntax:set Key=value
SETX Command Setting environment variables
SETX is an upgrade of set, which is used to set the persistent environment variable, which is the system environment variable, so that the variable can be used everywhere
Basic syntax:setx key value (note that there is no ' = ' number)
So in this case, if we want to send a message through another program or script, we need to set the environment variable using SETX:
This is basically OK, but I found that the results are still the value of none, this is also puzzled, access to data found there is no problem. After guessing may be a computer cause, need to restart to "activate" the environment variable, the result is restarted after the attempt to succeed,
Of course, in addition to the use of commands to configure environment variables, can also be manually configured: computer System/Advanced system settings/advanced/environment variables, specific configuration here no longer detailed, not quite understand the place can be self-search online
This is the configuration of the Flask-mail user name and password of some experience sharing, have any questions or ideas to welcome you in the comment area to learn from each other.
Python flask-mail environment variable configuration