Feb-02-2017, 09:58 AM
My python code is meant to send me an email with the string send from Arduino through the serial. Everything is working fine (Emails are sent when string is present in the serial port) except that, emails are sent continuously, instead of once. I receiving about 100s of emails with the same information. I need only one. I don't know how to fix it. Please help.
import time import serial import smtplib TO = '[email protected]' GMAIL_USER = '[email protected]' GMAIL_PASS = 'password' SUBJECT = 'ALERT!!!' ser = serial.Serial('COM4', 9600) message = ser.readline() TEXT = message def send_email(): print("Sending Email") server = smtplib.SMTP("smtp.gmail.com",587) server.ehlo() server.starttls() server.ehlo server.login(GMAIL_USER, GMAIL_PASS) header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER header = header + '\n' + 'Subject:' + SUBJECT + '\n' print header msg = header + '\n' + TEXT + ' \n\n' server.sendmail(GMAIL_USER, TO, msg) server.close() while True: print(message) send_email() time.sleep(0.5)