Search code examples
pythonsmtpsmtplib

Mail without password in python


I want to send mail to multiple ids using python. I am using smtplib to send it. I don't want to give my password in the script. But

smtp.login(username,password)

fails if I do not. Is there any other library to do so.


Solution

  • Where / how to store credentials is a broad / big question. One way I like is to use an environment variable like this:

    # In shell (to set the variable):
    $ export MY_SMTP_PASS="this is a secret password"
    
    # In python, to access it:
    import os
    smtp.login(username,os.environ['MY_SMTP_PASS'])
    

    Obviously there are a bunch of other things you might want to do, check if it's set before using (raise an exception), etc...