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.