0

I am using imaplib to read emails on my localhost through a python script. To do this I am using Xampp and MercuryMail.

import imaplib
import email

#takes args for subject to check for
def get_subject(subject):
    #most recent 15 emails
    for i in range( latest_email_id, latest_email_id-15, -1 ):
        typ, data = mail.fetch( str(i), '(RFC822)' )

        msg = email.message_from_string(data[0][1])

        varSubject = msg['subject']
        if subject == varSubject:
            return 1

mail = imaplib.IMAP4_SSL('localhost')
mail.login("user","pass")
mail.list()
mail.select("inbox")

typ, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()
latest_email_id = int( id_list[-1] )
typ, data = mail.fetch( str(latest_email_id), '(RFC822)' )
print(get_subject(subject = 'Test'))

mail.logout()

When I run this it returns an error saying connection actively refused. If I try to connect to gmail instead of localhost it connects fine. I have set up MercuryMail by adding the user and password to the settings. I also have another script that sends emails from localhost to localhost, and those emails go through. The script should get the latest 15 emails and check them for a specific subject, but I cant test it without being able to connect. I have never used python with emails or MercuryMail before so I may be missing something simple.

Zachary Burke
  • 125
  • 1
  • 1
  • 6

1 Answers1

0

Assuming there is an email server running on your host, which allows IMAP4 connections, my best bet is that it's not listening for connections on localhost. In other words, your host has at least one standard IP address (probably) plus the default localhost, but if the server is running on the standard address, and not on 127.0.0.1 or 0.0.0.0, then your connection will be refused.

SivanBH
  • 392
  • 3
  • 13