I'm trying write something that will return email body text once ran. What I have so far is:
from exchangelib import Credentials, Account
import urllib3
from bs4 import BeautifulSoup
credentials = Credentials('fake@email', 'password')
account = Account('fake@email', credentials=credentials, autodiscover=True)
for item in account.inbox.all().order_by('-datetime_received')[:1]:
html = item.unique_body
soup = BeautifulSoup(html, "html.parser")
for span in soup.find_all('font'):
return span.text
My issue is with the last line reading return span.text
. If I were to replace this line with print(span.text)
, it runs perfectly and prints the body text of the email. However when replaced with return
, it throws an error reading SyntaxError: 'return' outside function
. I've been digging into the issue, and I can't seem to figure out why it is throwing this issue. I'm new to Python and could use some help. What can I do to solve this?
As your error would indicate, you need to place your return
inside a function
from exchangelib import Credentials, Account
import urllib3
from bs4 import BeautifulSoup
credentials = Credentials('fake@email', 'password')
account = Account('fake@email', credentials=credentials, autodiscover=True)
def get_email(span): # a function that can return values
return span.text
for item in account.inbox.all().order_by('-datetime_received')[:1]:
html = item.unique_body
soup = BeautifulSoup(html, "html.parser")
for span in soup.find_all('font'):
email_result = get_email(span) # call function and save returned value in a variable