Hello!
Great platform - love it!
I have used a script to automatically get images from a public website and send out at a scheduled time. The script runs fine when I run from within Bash environment of PA, but fails on scheduled task. Was wondering if someone could help me with why this may be?
Apologies, the script is rather long - have tried to shorten the best I can whilst still reproducing the error.
# Import modules
from datetime import date, timedelta
import os
import requests
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib
# Create a dict of file-name and location on web
file_list = {
"WA_Rainfall.gif" : "http://www.bom.gov.au/fwo/IDW62203.gif",
"SA_Rainfall.gif" : "http://www.bom.gov.au/fwo/IDS65036.gif",
}
# Use Requests to download gif file
for k, url in file_list.items():
r = requests.get(url, allow_redirects=True)
with open(k, 'wb') as fd:
fd.write(r.content)
msg = MIMEMultipart('related')
d={}
i=0
for k,v in file_list.items():
i+=1
fp = open(k, 'rb')
d["string{0}".format(i)] = MIMEImage(fp.read())
fp.close()
d["string{0}".format(i)].add_header('Content-ID', '<{}>'.format(v))
msg.attach(d["string{0}".format(i)])
fromaddr = 'XXXX@XXXX.com'
toaddr = ['XXXX@XXXXX.com']
subject = 'Evening AU Weather Maps'
msg['From'] = fromaddr
msg['To'] = ", ".join(toaddr)
msg['Subject'] = subject
smtpObj = smtplib.SMTP('smtp.office365.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login("XXXX@XXXX.com","XXXXX")
smtpObj.sendmail(fromaddr, toaddr, msg.as_string().encode('utf-8'))
The script throws the following error in the log:
Traceback (most recent call last): File "/home/dimariyasinghe/TestSnippet.py", line 57, in <module> d["string{0}".format(i)] = MIMEImage(fp.read()) File "/usr/lib/python3.6/email/mime/image.py", line 43, in init raise TypeError('Could not guess image MIME subtype') TypeError: Could not guess image MIME subtype
2018-06-29 01:23:09 -- Completed task, took 5.00 seconds, return code was 1.
Any help greatly appreciated!
Thanks
Dim