-1

Hi I am getting this error when running script. I dont know whats wron with code.

File "C:\Users\User\outlook.py", line 25
    print "---------------------------------------------------------"
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("---------------------------------------------------------")?
import imaplib
import email

word = ["http://", "https://", "www.", ".com", "deutschesexkontakte.com"] #list of strings to search for in email body

#connection to the email server
mail = imaplib.IMAP4_SSL('outlook.office365.com')
mail.login('email@outlook.com', 'password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("Inbox", readonly=True) # connect to inbox.

result, data = mail.uid('search', None, "ALL") # search and return uids instead

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_uid = data[0].split()[-1]

result, data = mail.uid('fetch', latest_email_uid, '(RFC822)') # fetch the email headers and body (RFC822) for the given ID


raw_email = data[0][1] # here's the body, which is raw headers and html and body of the whole email
# including headers and alternate payloads

print "---------------------------------------------------------"
print "Are there links in the email?"
print "---------------------------------------------------------"

msg = email.message_from_string(raw_email)
for part in msg.walk():
    # each part is a either non-multipart, or another multipart message
    # that contains further parts... Message is organized like a tree
    if part.get_content_type() == 'text/plain':
        plain_text = part.get_payload()
        print plain_text # prints the raw text
        if any(word in plain_text for word in word):
            print '****'
            print 'found link in email body'
            print '****'
        else:
            print '****'
            print 'no link in email body'
            print '****'`
Max
  • 10,701
  • 2
  • 24
  • 48
Stefan
  • 17
  • 2
  • 4
    Python 2 code run with python 3? – rdas May 29 '21 at 13:41
  • You might want to remove your personal data from the code including credentials and website preferences. – Klaus D. May 29 '21 at 14:39
  • You will also want to use email_from_bytes() with python 3. You should probably try to find a more modern sample. – Max May 29 '21 at 15:03
  • Does this answer your question? [What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?](https://stackoverflow.com/questions/25445439/what-does-syntaxerror-missing-parentheses-in-call-to-print-mean-in-python) – Vladimir May 31 '21 at 09:01
  • Hi! These credentials are Bulk Emails, How ever, Thanks for you help! Is it a big deal to rewrite this code to python3? – Stefan May 31 '21 at 18:49

1 Answers1

1

You need replace print from:

print "---------"

to:

print("---------")

And with another print too.

Vlad Anoxun
  • 78
  • 1
  • 5