0

I am able to log into my email, search my inbox for any unread messages, and print the body of the unread messages but I am trying to write the answers to my questionnaire to a pandas dataframe. Example of questionnaire email when I receive it:

name --- Brook Yohohoho ::: 
email --- deeew32@yahoo.com ::: 
mobile --- 123-458-9463  ::: 

There are 24 questions total.

My code for the section of retrieving the email and trying to write it to a csv is:

try:
    for num in data[0].split():
        typ, msg_data = conn.fetch(num, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                msg = email.message_from_string(response_part[1])
                subject=msg['subject']
                print(subject)
                payload=msg.get_payload()
                body=extract_body(payload)
                print(body)
                df = pd.DataFrame(eval(body), delimiter = '[,,,:::]', columns = ['name', 'email', 'mobile'])
                df.to_csv("questionnaire.csv", delimiter = ',')
                print(df)
        typ, response = conn.store(num, '+FLAGS', r'(\Seen)') 

I only want to write the answeres to the questions to the dataframe. The body of the email will not write to the dataframe and I get the error:

Traceback (most recent call last):
 File "email3.py", line 27, in <module>
  df = pd.DataFrame(eval(body), delimiter = '[,,,:::]', columns = ['name', 'email', 'mobile'])
 File "<string>", line 1
  name --- Brook Yohohoho :::
                        ^
SyntaxError: invalid syntax

Side note**** All emails to this email address will be the answers to the questionnaire. The format that the emails are sent can be changed if necessary. For ex, instead of each line of the email being "name --- Brook Yohohoho ::: " it can be changed to just the name "Brook Yohohoho" etc.

rzaratx
  • 756
  • 3
  • 9
  • 29

0 Answers0