9

Running on Windows 7 and using PyCharm 2016.2.3 if that matters at all.

Anyway, I'm trying to write a program that sends an email to recipients, but I want the console to prompt for a password to login.

I heard that getpass.getpass() can be used to hide the input.

Here is my code:

import smtplib
import getpass

import sys

print('Starting...')

SERVER = "localhost"
FROM = "my@email.com"

while True:
    password = getpass.getpass()
    try:
        smtpObj = smtplib.SMTP(SERVER)
        smtpObj.login(FROM, password)
        break
    except smtplib.SMTPAuthenticationError:
        print("Wrong Username/Password.")
    except ConnectionRefusedError:
        print("Connection refused.")
        sys.exit()

TO = ["your@email.com"] 
SUBJECT = "Hello!"
TEXT = "msg text"

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

smtpObj.sendmail(FROM, TO, message)
smtpObj.close()
print("Successfully sent email")

But when I run my code, here is the output:

 Starting...
 /Nothing else appears/

I know the default prompt for getpass() is 'Password:' but I get the same result even when I pass it a prompt string.

Any suggestions?

EDIT: The code continues to run indefinitely after it prints the string, but nothing else appears and no emails are sent.

TheDude
  • 93
  • 1
  • 1
  • 5

2 Answers2

22

For PyCharm 2018.3 Go to 'Edit Configurations' and then select 'Emulate terminal in output console'.

enter image description here

Answer provided by Abhyudaya Sharma

guerda
  • 23,388
  • 27
  • 97
  • 146
  • 1
    Worked in debug for me both with and without checking this option. Only difference is without it checked, it actually showed the password I typed. BTW, I was using Intellij with Python plugin. – codingjeremy Sep 04 '19 at 21:20
19

The problem you have is that you are launching it via PyCharm, which has it's own console (and is not the console used by getpass)

Running the code via a command prompt should work

UnholySheep
  • 3,967
  • 4
  • 19
  • 24
  • 4
    Is there a `getpass()` equivalent that can run on the PyCharm console? – TheDude Oct 13 '16 at 12:37
  • 2
    None that I know of. Also running the script in debug mode in PyCharm might work as well (It worked on my machine but I saw reports that that is not always the case) – UnholySheep Oct 13 '16 at 16:50