0

I am trying to input my both account and password to the system and then wanna return the status of email login, True for success login. Now I am using poplib to login the account and return the email stat. Are there any method to return the login status by poplib at the same time (Or not by poplib)? Thanks.

import poplib
server = poplib.POP3(pop3_server)
print(server.getwelcome())
server.user(email)
server.pass_(password)
print('Messages: %s. Size: %s' % server.stat())
server.quit()
WILLIAM
  • 457
  • 5
  • 28
  • take a look @ https://stackoverflow.com/questions/8669202/get-emails-with-python-and-poplib – Jibin Mathews Feb 27 '19 at 07:19
  • @JibinMathews Thanks, I know how to connect server and also the status for success now. However, if the email is wrong, I have no idea how to throw the error as shown below. – WILLIAM Feb 27 '19 at 08:26

1 Answers1

0

Try the following code:

import poplib
server = poplib.POP3_SSL('pop.googlemail.com', '995')
print(server.getwelcome())
server.user(email)
try:
   status = server.pass_(pass)
   print('Messages: %s. Size: %s' % server.stat())
except Exception as error:
   status = "Not OK"
   print(error)

if "+OK" in status:
    print ("Login Successful!!") # Return True
else:
    print ("Login Failed!!") # Return False

server.quit()

Output in case of Error:

+OK Gpop ready for requests from 114.143.178.34 c14mb53749528ybi
-ERR [AUTH] Username and password not accepted.
Login Failed!!

Hope this answers your question!!

skaul05
  • 2,154
  • 3
  • 16
  • 26
  • Thanks , however when I tried to login a wrong email account, it cannot throw the error and stopped program. I also try to throw it like "except poplib.error_proto", but its not working. – WILLIAM Feb 27 '19 at 08:16
  • The error is shown as below: Traceback (most recent call last): File "check_user_login_info.py", line 32, in check_mail(email, password, pop3_server) File "check_user_login_info.py", line 13, in check_mail except poplib.error_proto('-ERR EOF'): TypeError: catching classes that do not inherit from BaseException is not allowed – WILLIAM Feb 27 '19 at 08:18
  • Why do you want to throw an error when login fails. At failure, it will go into `except `block and eventually print `Login Failed`. However I have edited my solution to print error – skaul05 Feb 27 '19 at 08:51
  • Sorry, it doesnt work for me. First, when I add the line "status = server.pass_(p)", the program just keep loading and I have to stop it manually. Besides, when change the line back to "server.pass_(p)" and login with wrong password, the error is detected, but it doesnt go to the except block and simply stopped the program. I have no idea why it happen. – WILLIAM Feb 28 '19 at 01:56
  • Are you using the whole code in my solution or do u have made any changes – skaul05 Feb 28 '19 at 02:20
  • Yea exactly. But just now, I change a bit your code and move the if - statement before the server.quit(). It works now. Thanks a lot. – WILLIAM Feb 28 '19 at 03:16