I have a for loop that works perfectly like so:
from exchangelib import DELEGATE, Account, Configuration, Credentials, NTLM
from bs4 import BeautifulSoup
from exchangelib.properties import NewMailEvent, MovedEvent
from exchangelib_listener import Listener
import re
credentials = Credentials('[email protected]', 'EDDBIGHEAD90!')
config = Configuration(server='east.EXCH091.conglomo.net', credentials=credentials)
account = Account('[email protected]', credentials=credentials, config=config, autodiscover=False)
listener = Listener(account)
for item in account.inbox.all().order_by('-datetime_received')[:1]:
soup = BeautifulSoup(item.body).get_text()
print(soup) #this will print
This will ouput:
"""Last email's body information"""
However, if I try to add that into a function so the event listener can pick up on it, the forloop doesn't print anything.
from exchangelib import DELEGATE, Account, Configuration, Credentials, NTLM
from bs4 import BeautifulSoup
from exchangelib.properties import NewMailEvent, MovedEvent
from exchangelib_listener import Listener
import re
credentials = Credentials('[email protected]', 'EDDBIGHEAD90!')
config = Configuration(server='east.EXCH091.conglomo.net', credentials=credentials)
account = Account('[email protected]', credentials=credentials, config=config, autodiscover=False)
listener = Listener(account)
def new_messaged_received():
print("test") #This will print
for item in account.inbox.all().order_by('-datetime_received')[:1]:
soup = BeautifulSoup(item.body).get_text()
print(soup) #This will not print
listener.streaming_event_received += new_messaged_received
listener.listen(NewMailEvent)
Let me know if I am missing anything to get that forloop to work inside the function so I can append it to the listener, if that is possible.
Thanks
I figured out what to do as a work around but I think there is some confliction going on with the exchange-lib-listener
and the exchange-lib
modules.
Basically, I had to put my for loop into its own python file and call it using subprocess.call. If I did any other methods, the for loop would not work.
listener script:
from exchangelib import DELEGATE, Account, Configuration, Credentials, NTLM
from exchangelib.properties import NewMailEvent, MovedEvent
from exchangelib_listener import Listener
import subprocess
credentials = Credentials('[email protected]', 'EDDBIGHEAD90!')
config = Configuration(server='east.EXCH091.conglomo.net', credentials=credentials)
account = Account('[email protected]', credentials=credentials, config=config, autodiscover=False)
listener = Listener(account)
def new_messaged_received():
print("test") #This will print
subprocess.call(['python3', '/home/tech/scripts/Testing/forloopemail.py'])
listener.streaming_event_received += new_messaged_received
listener.listen(NewMailEvent)
Forloopscript:
from exchangelib import DELEGATE, Account, Configuration, Credentials, NTLM
from bs4 import BeautifulSoup
credentials = Credentials('[email protected]', 'EDDBIGHEAD90!')
config = Configuration(server='east.EXCH091.conglomo.net', credentials=credentials)
account = Account('[email protected]', credentials=credentials, config=config, autodiscover=False)
listener = Listener(account)
def emailcheck():
print("test2")
for item in accountm.inbox.all().order_by('-datetime_received')[:1]:
soup = BeautifulSoup(item.body).get_text()
print(soup)
emailcheck()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments