Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import imaplib, smtplib, email
- from datetime import datetime
- # --- KONFIGURATION ---
- IMAP_SERVER = "imap.server.invalid"
- IMAP_USER = "[email protected]"
- IMAP_PASS = "yxayxa"
- SMTP_SERVER = "outgoing.server.invalid"
- SMTP_PORT = 587
- SMTP_USER = "[email protected]"
- SMTP_PASS = "lrxlrx"
- FORWARD_TO = "[email protected]"
- # ----------------------
- def check_and_forward():
- try:
- now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- mail = imaplib.IMAP4_SSL(IMAP_SERVER)
- mail.login(IMAP_USER, IMAP_PASS)
- mail.select("INBOX")
- status, messages = mail.search(None, 'UNSEEN')
- email_ids = messages[0].split()
- # NEU: Kurze Rückmeldung bei jedem Durchlauf
- print(f"[{now}] [*] Posteingang geprüft. Ungelesene Mails: {len(email_ids)}")
- if email_ids:
- smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
- smtp.starttls()
- smtp.login(SMTP_USER, SMTP_PASS)
- for e_id in email_ids:
- status, msg_data = mail.fetch(e_id, '(RFC822)')
- for response_part in msg_data:
- if isinstance(response_part, tuple):
- msg = email.message_from_bytes(response_part[1])
- del msg['Resent-To']
- msg['Resent-To'] = FORWARD_TO
- smtp.sendmail(SMTP_USER, [FORWARD_TO], msg.as_string())
- print(f"[{now}] [+] Weitergeleitet: {msg.get('Subject', 'Kein Betreff')}")
- mail.store(e_id, '+FLAGS', '\\Seen')
- smtp.quit()
- mail.logout()
- except Exception as e:
- print(f"[{now}] [-] Fehler: {e}")
- if __name__ == "__main__":
- check_and_forward()
Advertisement
Add Comment
Please, Sign In to add comment