For loop returns nothing when using function

SOUser418

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

SOUser418

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.

edited at
0

Comments

0 comments
Login to comment

Related

ANY function returns nothing when the database is empty

Plotly displayed nothing when using for loop in a notebook

*ngFor using a function, returns a loop

python function returns nothing

Return function returns nothing

I want to repeat a function using for loop but the for loop does nothing

Serializer using Normalizer returns nothing when using setCircularReferenceHandler

Create a completion block that returns nothing but executes another function when completed?

onBeforeUnload creates prompt even when function returns nothing

For loop returns nothing React JS

Loop through IEnumerable returns nothing

Query Returns Nothing when using SqlParamter: Entity Framework

Alternative of using try catch for an axios request when there is an error that returns nothing?

Break an infinite loop when a function returns True

How to create function that returns nothing

Why XMPP function returns nothing?

Laravel hasmany function returns nothing

void function returns nothing - C

Calling a stored function that returns nothing

unit test for a function that returns nothing

Using for loop in a function, only returns last value

Function using for loop returns different values

Function that only returns false returns nothing

when using shell_exec() function in PHP nothing happens

Web scraping program for loop returns nothing

Why "c:forEach" loop returns nothing in this jsp?

Why won’t C compiler give a warning when writing a function which returns nothing and has no side effect?

Non-void function returns nothing dart

importing and calling a function in Typsescript which returns nothing