Forums

Email script using smtplib stopped working

I have a scheduled python script that was working fine in April. Then on May 1st it stopped working. The script still runs without an error but the message body is empty. Any idea why?

Also I tried running via the console and had the same problem. My code:

 # import smtplib for the actual sending function
import smtplib
import datetime

now = datetime.datetime.now()
todays_date = str(now.month) + '-' + str(now.day) + '-' + str(now.year)

# import the email modules we'll need
from email.mime.text import MIMEText

# open a plain text file for reading. 
textfile = 'rtm-completed'+ todays_date + '.txt'
print textfile

fp = open(textfile, 'rb')
# create a plain text message

msg = MIMEText(fp.read())
fp.close
print msg.as_string()

me = 'email@gmail.com'
you = 'trigger@recipe.ifttt.com'
msg['Subject'] = 'Completed Tasks Today'
msg['From'] = me
msg['To'] = you

username = 'email'
password = 'password'

# send the message via our own SMTP server, but don't include the
# envelope header
s = smtplib.SMTP('smtp.gmail.com:587')
s.ehlo()
s.starttls()
s.login(username, password)
s.sendmail(me, you, msg.as_string())
s.quit()

[edit by admin: formatting]

I don't think the problem is with the email script -- I'd suspect it's with whatever is writing your text files. Do the files for 1 May onwards have any contents?

They do which is why it so confounding. They're just plain text files.

Could there be some kind of race condition between the job that's writing the files and the job that's emailing them?