0

I have a list of emails(mine) that I want to test against a list of passwords(All valid and some none valid of course) using imaplib library. Whenever I test the program ordinarily like in the code below, it works perfectly no errors.

import sys
import imaplib

# connect to host using SSL
imap_host = 'imap.server.com'
imap_port = '993'
imap_user = 'username@email'
imap_pass = 'RightPassword'

imap = imaplib.IMAP4_SSL(imap_host, imap_port)

## login to server

try:
    login = imap.login(imap_user, imap_pass)

    if login:
        print login
except imaplib.IMAP4.error as error:
    print error

#

But whenever I run the code such as to parsing credentials through a function to handle the authentication protocols such as the following code below, I get an error saying

"LOGIN command error: BAD ['Missing \'"\'']".

I have tried all sort of things I could find using google and non seem to handle it properly.

"""
    E-mail Tester
    NB: This is for educational purpose only.
                            """

import sys
import imaplib


EMAILS_FILE = open('email_list.txt', 'r')

PASSWORD_FILE = open('pass_list.txt', 'r')

SUCCESS_FILE = open('success.txt', 'a')

EMAILS_FILE_LIST = []


def set_check(_emails):

    email = str(_emails)

    PASSWORD_FILE.seek(0)

    for passwords in PASSWORD_FILE:

        password = str(passwords)

        # connect to host using SSL
        imap_host = 'imap.server.com'
        imap_port = '993'
        imap = imaplib.IMAP4_SSL(imap_host, imap_port)

        ## login to server

        try:
            # print "%s%s" % (email,password)
            # print "I got here so far"
            # sys.exit()

            print "Testing <--> E-mail: %s - Password: %s" % (email, password)

            login = imap.login("%s","%s" % (email, password))

            if login:
                print login
                print "OK <---> E-mail: %s\nPassword: %s" % (email, password) 
        except imaplib.IMAP4.error as error:
            print error

for emails in EMAILS_FILE:
    EMAILS_FILE_LIST.append(emails)

for email_count in range(0, len(EMAILS_FILE_LIST)):
    set_check(EMAILS_FILE_LIST[email_count])

I have tried all kind of suggestions I could find on the internet but non has worked thus far.

I expect imap.login to handle the authentication without the mysterious error output

"LOGIN command error: BAD ['Missing \'"\'']"

glibdud
  • 7,550
  • 4
  • 27
  • 37

2 Answers2

0

login = imap.login("%s","%s" % (email, password))

does not work. It throws an error in Python: TypeError: not all arguments converted during string formatting, because you're providing two strings to one %s.

Why don't you just use imap.login(email, password)? It has the same effect as what you're trying to do.

And what does your password file look like? What is it actually sending? Please provide the log line before it crashes. (anonymizing if necessary, but leaving any punctuation in for help diagnosing)

Max
  • 10,701
  • 2
  • 24
  • 48
  • Thanks for your response Max, imap.login("%s","%s" % (email, password)) actually is one of the many things I have tried to make it work when imap.login(email, password) failed. My password file for a test have a list of 10 wrong passwords with one right password in between. The code doesn't crash, it just continues to print LOGIN command error: BAD ['Missing \'"\''] for each test until the password list is exhausted. – Declan Shoots Jul 24 '19 at 16:22
  • In the future, please be sure the code you post matches the results you get. The code as posted would just crash. – Max Jul 24 '19 at 17:15
0

Okay, so I actually got this fixed by removing trail lines from my strings.

email = str(_emails).rstrip()

PASSWORD_FILE.seek(0)

for passwords in PASSWORD_FILE:

    password = str(passwords).rstrip()

the error is caused by trail lines in the strings.