What is a secure way to send an email using Python and Gmail as the provider?

10,904

Solution 1

The connection uses STARTTLS, so its not being sent over the internet in clear text.

The function server.starttls() starts the encrypted communication with the server on port 465 instead of the normal port 25 for unencrypted SMTP mail traffic.

Solution 2

An obvious solution would be to use

getpass.getpass()

to get the password at the start of running, and store that in memory.

Share:
10,904
dot_zero
Author by

dot_zero

Updated on June 05, 2022

Comments

  • dot_zero
    dot_zero almost 2 years

    I am trying to send emails to myself using a Python script, and luckily I came across this post:

    How to send an email with Gmail as provider using Python?

    The trouble is, smtplib sends out the password for the script in plain text and I am skeptical about its security. Further my script contains my username and password in plain text. Are there any good ways to use Python and send emails without having to keep my password as plain text?

    I also saw this on StackOverflow: Python smtplib security but the answer is not completely helping me resolve this conflict. However, I'm not ready to give up yet.


    Some more information: I'm trying to set up my Raspberry Pi as a server that scrapes through a website. When a specific thing about the website changes, I want to be notified via email. However, I don't want to leave my Pi sitting around with a script that has my username and password in plain text.

  • Max
    Max over 9 years
    Or use oauth2, which is actually how Google wants applications to identify themselves. However, that takes a lot of infrastructure. For personal use, an app specific password would be best.
  • Anentropic
    Anentropic over 9 years
    looks like the easiest way might by Google's own gmail API python client developers.google.com/api-client-library/python/apis/gmail/v‌​1
  • shlgug
    shlgug over 6 years
    To clarify, add import getpass at the beginning of the file. Then do something like email_password = getpass.getpass("Enter Email Password: ").