import RPi.GPIO as GPIO import email import imaplib import smtplib import time from email.mime.text import MIMEText user = 'user@domain1.de' password = 'password' recipient="test@domain.de" IMAP = 'imap.provider.de' SMTP = 'smtp.provider.de' erlaubt = ["test1@domain1.de","test2@domain3.de","test3@domain3.de"] GPIO.setmode(GPIO.BCM) RELAIS=26 GPIO.setup(RELAIS, GPIO.OUT) mail = imaplib.IMAP4_SSL(IMAP) mail.login(user, password) mail.select('inbox') status, data = mail.search(None, 'ALL') mail_ids = [] for block in data: mail_ids += block.split() for i in mail_ids: status, data = mail.fetch(i, '(RFC822)') for response_part in data: if isinstance(response_part, tuple): message = email.message_from_bytes(response_part[1]) mail_from = message['from'].lower() mail_subject = message['subject'].lower() if message.is_multipart(): mail_content = '' for part in message.get_payload(): if part.get_content_type() == 'text/plain': mail_content += part.get_payload() else: mail_content = message.get_payload() ok=0 for e in erlaubt: if e in mail_from: ok=1 if ok==1: print(mail_from + " berechtigt. Betreff: " + mail_subject) nachricht = email.mime.text.MIMEText('Nur zur Info') if "open garage" in mail_subject: GPIO.output(RELAIS,True) time.sleep(1) GPIO.output(RELAIS,False) nachricht['Subject'] = 'Garage geƶffnet' nachricht['From'] = user nachricht['To'] = recipient server = smtplib.SMTP_SSL(host=SMTP, port=465) server.login(user, password) server.sendmail(user, [recipient], nachricht.as_string()) server.quit() else: print("E-Mail Adresse " + mail_from + " NICHT erlaubt") mail.store(i, "+FLAGS", "\\Deleted") mail.expunge() mail.close() mail.logout() GPIO.cleanup()