From 776bf74e4ffa74b2a942e92a539063f9f7659ea2 Mon Sep 17 00:00:00 2001 From: Orlando Alexander <67097862+orlandoalexander@users.noreply.github.com> Date: Fri, 20 Jan 2023 15:56:52 +0000 Subject: [PATCH] Update email_file.py Signed-off-by: Orlando Alexander <67097862+orlandoalexander@users.noreply.github.com> --- code/email_file.py | 66 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/code/email_file.py b/code/email_file.py index f0b52b0..f3e04ed 100644 --- a/code/email_file.py +++ b/code/email_file.py @@ -28,40 +28,40 @@ while True: # continually try to send email time.sleep(10) # delay to allow Raspberry Pi to connect to internet - try: - # connect to gmail server: - session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) - session.ehlo() - session.starttls() - session.ehlo() + #try: + # connect to gmail server: + session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) + session.ehlo() + session.starttls() + session.ehlo() - # login to gmail - session.login(GMAIL_USERNAME, GMAIL_PASSWORD) + # login to gmail + session.login(GMAIL_USERNAME, GMAIL_PASSWORD) - msg = MIMEMultipart() # create message data - msg['Subject'] = SUBJECT # assign message subject + msg = MIMEMultipart() # create message data + msg['Subject'] = SUBJECT # assign message subject - count = 0 - directory = '/home/ecoswell/RaspberryPi-Sensor/data_final' # directory storing data files ready to be emailed - for file in os.listdir(directory): # iterate over each file ready to be emailed - count+=1 - filename = os.path.join(directory, file) - with open(filename, "rb") as f: # open file to be emailed - part = MIMEApplication(f.read(),Name=basename(filename)) - part['Content-Disposition'] = 'attachment; filename="%s"' % basename(filename) - msg.attach(part) # add file as email attachement - - if count != 0: # if zero files in data folder: - # send email: - session.sendmail(GMAIL_USERNAME, RECIPIENT, msg.as_string()) # send email to desired email address - session.quit + count = 0 + directory = '/home/ecoswell/RaspberryPi-Sensor/data_final' # directory storing data files ready to be emailed + for file in os.listdir(directory): # iterate over each file ready to be emailed + count+=1 + filename = os.path.join(directory, file) + with open(filename, "rb") as f: # open file to be emailed + part = MIMEApplication(f.read(),Name=basename(filename)) + part['Content-Disposition'] = 'attachment; filename="%s"' % basename(filename) + msg.attach(part) # add file as email attachement - # move each emailed file to directory storing emails which have already been emailed - new_directory = '/home/ecoswell/RaspberryPi-Sensor/data_emailed' - for file in os.listdir(directory): - filename = os.path.join(directory, file) - new_filename = os.path.join(new_directory, file) - os.rename(filename, new_filename) - break - except: - pass + if count != 0: # if zero files in data folder: + # send email: + session.sendmail(GMAIL_USERNAME, RECIPIENT, msg.as_string()) # send email to desired email address + session.quit + + # move each emailed file to directory storing emails which have already been emailed + new_directory = '/home/ecoswell/RaspberryPi-Sensor/data_emailed' + for file in os.listdir(directory): + filename = os.path.join(directory, file) + new_filename = os.path.join(new_directory, file) + os.rename(filename, new_filename) + break +# except: +# pass