mit dem nachfolgenden Code rufe ich Mails mit einem bestimmten Betreff ab und ziehe mir dann aus dem Text einen Namen.
Soweit klappt das auch, solange der Name keine Umlaute hat oder ggf. Sonderzeichen.
Wie bekommen ich das gelöst?
Vorab vielen Dank für Tipps und Ratschläge!
Code: Alles auswählen
with imaplib.IMAP4_SSL(host="mail.server.de", port=imaplib.IMAP4_SSL_PORT) as imap_ssl:
resp_code, response = imap_ssl.login("xyz@xyz.de", "Passwort")
resp_code, mail_count = imap_ssl.select(mailbox="Inbox", readonly=True)
resp_code, mails = imap_ssl.search(None, '(UNSEEN SUBJECT "%s")' % subject)
for mail_id in mails[0].decode().split()[-2:]:
resp_code, mail_data = imap_ssl.fetch(mail_id, '(RFC822)') ## Fetch mail data.
message = email.message_from_bytes(mail_data[0][1]) ## Construct Message from mail data
for part in message.walk():
if part.get_content_type() == "text/plain":
body_lines = part.as_string().split("\n")
content = "\n".join(body_lines)
#Get customer name
startStringBefore = 0
stopStringBefore = content.index(stringBefore) + len(stringBefore)
if len(content) > stopStringBefore:
content = content[0: startStringBefore:] + content[stopStringBefore +1::]
start1 = content.index(stringAfter)
stop1 = len(content)
if len(content) > stopStringBefore:
content = content[0: start1:] + content[stop1 +1::]
content = content.strip()
print(content)
imap_ssl.close()