I'm relatively new to Python (and development in general) so would appreciate any help that can be offered.
I've built a script which runs on my local machine, and I want to run it on my webserver if possible as a scheduled task.
The script checks for new emails in a dedicated email account, and if certain criteria are met against the subject, a textfile is appended with taskpaper formatted text, which I will have a workflow (iOS) setup to add these tasks into omnifocus.
I was originally getting the following error:
NameError: name 'email' is not defined
So I added 'import email', and now I get the following error:
subject = msg['subject'].split()
AttributeError
:
'NoneType' object has no attribute 'split'
End of script output before headers: omnifocus.py
I'm not sure where I'm going wrong, as it works locally but not on my server. I can set up a raspberry pi for this, but since I already pay for a webserver, would be great if it could run this for me.
My Code:
#! /bin/usr/python
import imaplib
import email
ORG_EMAIL = "@xxx.com"
FROM_EMAIL = "omnifocus" + ORG_EMAIL
FROM_PWD = "xxxxxxxxxx"
SMTP_SERVER = "smtp.stackmail.com"
SMTP_PORT = 993
IMAP_SERVER = 'imap.stackmail.com'
mail = imaplib.IMAP4_SSL(SMTP_SERVER)
mail.login(FROM_EMAIL,FROM_PWD)
mail.select('inbox')
type, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
result = []
for i in id_list:
typ, data = mail.fetch(i, '(RFC822)' )
msg = email.message_from_string(data[0][1].decode('utf-8'))
subject = msg['subject'].split()
if subject[-1] == 'Accepted':
job_id = subject[-2]
tp = 'Quote ' + job_id + ' Accepted @autodone(true)\n\t- Complete Site Report @due(5:00 pm) @tag(Work : Computer)\n\t\t' + 'http://xxxxxxx.com?ARGUMENTS=-ALITEJOBDETAIL&jobid=' + job_id
result.append(tp)
elif subject[-2] == 'Quote':
job_id = subject[-1]
tp = 'Quote ' + job_id + ' Sent @autodone(true)\n\t- 3 day follow up @due(3 days 5:00 pm) @tag(iPhone : Calls)\n\t\t' + 'http://xxxxxxx.com?ARGUMENTS=-ALITEJOBDETAIL&jobid=' + job_id + '\n\t- 7 day follow up @due(7 days 5:00 pm) @tag(iPhone : Calls)\n\t\t' + 'http://xxxxxxx.com?ARGUMENTS=-ALITEJOBDETAIL&jobid=' + job_id + '\n\t- 13 day follow up @due(13 days 5:00 pm) @tag(iPhone : Calls)\n\t\t' + 'http://xxxxxxx.com?PRGNAME=master&ARGUMENTS=-ALITEJOBDETAIL&jobid=' + job_id
result.append(tp)
# delete emails, deactivated during test stage
#mail.store(i, '+FLAGS', '\\Deleted')
taskpaper = '\n'.join(result)
with open('taskpaper.txt', mode='a') as txt:
f.write('\n' + taskpaper)
import emailstatement to work.