URL mit IMAP aus der email in eine TXT Datei kopieren

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Stefan12121212
User
Beiträge: 1
Registriert: Donnerstag 10. Juni 2021, 15:06

Guten Tag!
Ich habe hier einen Python script, der mir die email mit IMAP decoded anzeigt.
Als Nächstes möchte ich, dass automatisiert die URL kopiert wird.
Hat jemand eine Idee wonach ich recherchieren sollte damit mein Vorhaben funktioniert?
Danke!






Code: Alles auswählen

 import imaplib
import email
from email.header import decode_header
import webbrowser
import os

# account credentials
username = "EMAIL"
password = "PASSWORD"

def clean(text):
    # clean text for creating a folder
    return "".join(c if c.isalnum() else "_" for c in text)



# create an IMAP4 class with SSL 
imap = imaplib.IMAP4_SSL("outlook.office365.com")
# authenticate
imap.login(username, password)


status, messages = imap.select("INBOX")
# number of top emails to fetch
N = 3
# total number of emails
messages = int(messages[0])


for i in range(messages, messages-N, -1):
    # fetch the email message by ID
    res, msg = imap.fetch(str(i), "(RFC822)")
    for response in msg:
        if isinstance(response, tuple):
            # parse a bytes email into a message object
            msg = email.message_from_bytes(response[1])
            # decode the email subject
            subject, encoding = decode_header(msg["Subject"])[0]
            if isinstance(subject, bytes):
                # if it's a bytes, decode to str
                subject = subject.decode(encoding)
            # decode email sender
            From, encoding = decode_header(msg.get("From"))[0]
            if isinstance(From, bytes):
                From = From.decode(encoding)
            print("Subject:", subject)
            print("From:", From)
            # if the email message is multipart
            if msg.is_multipart():
                # iterate over email parts
                for part in msg.walk():
                    # extract content type of email
                    content_type = part.get_content_type()
                    content_disposition = str(part.get("Content-Disposition"))
                    try:
                        # get the email body
                        body = part.get_payload(decode=True).decode()
                    except:
                        pass
                    if content_type == "text/plain" and "attachment" not in content_disposition:
                        # print text/plain emails and skip attachments
                        print(body)
                    elif "attachment" in content_disposition:
                        # download attachment
                        filename = part.get_filename()
                        if filename:
                            folder_name = clean(subject)
                            if not os.path.isdir(folder_name):
                                # make a folder for this email (named after the subject)
                                os.mkdir(folder_name)
                            filepath = os.path.join(folder_name, filename)
                            # download attachment and save it
                            open(filepath, "wb").write(part.get_payload(decode=True))
            else:
                # extract content type of email
                content_type = msg.get_content_type()
                # get the email body
                body = msg.get_payload(decode=True).decode()
                if content_type == "text/plain":
                    # print only text email parts
                    print(body)
            if content_type == "text/html":
                # if it's HTML, create a new HTML file and open it in browser
                folder_name = clean(subject)
                if not os.path.isdir(folder_name):
                    # make a folder for this email (named after the subject)
                    os.mkdir(folder_name)
                filename = "index.html"
                filepath = os.path.join(folder_name, filename)
                # write the file
                open(filepath, "w").write(body)
                # open in the default browser
                webbrowser.open(filepath)
            print("="*100)
# close the connection and logout
imap.close()
imap.logout()
Sirius3
User
Beiträge: 17711
Registriert: Sonntag 21. Oktober 2012, 17:20

Von welcher URL sprichst Du?

Der Code ist viel zu unstrukturiert. Eine for-Schleife mit 60 Zeilen ist viel zu lang. Das muß man in mehrere Funktionen aufteilen. Dass das zu viel ist, sieht man auch einfach daran, dass Du 9 Einrückebenen benutzt.

Konstanten schreibt man KOMPLETT_GROSS. Alle Variablen schreibt man dagegen komplett klein.
Dateien, die man öffnet, muß man auch wieder schließen.
Nackte excepts benutzt man nicht. Fange immer die Exception so konkret wie möglich ab.
Man wandelt keine None in einen String um, nur um dann mit ›in‹ auf irgendwas zu testen.
Dann hast Du noch Einrückfehler.

Code: Alles auswählen

import imaplib
import email
from email.header import decode_header
from pathlib import Path
import webbrowser

# account credentials
USERNAME = "EMAIL"
PASSWORD = "PASSWORD"

N = 3

def clean(text):
    # clean text for creating a folder
    return "".join(c if c.isalnum() else "_" for c in text)


def decode(data):
    return "".join(
        text if isinstance(text, str) else text.decode(encoding or "ASCII")
        for text, encoding in decode_header(data)
    )


def write_attachement(path, part):
    filename = part.get_filename()
    if not filename:
        filename = "unknown"
    payload = part.get_payload(decode=True)
    path.mkdir(parents=True, exist_ok=True)
    # TODO: check filename validity
    (path / filename).write_bytes(payload)

def write_body(path, part):
    filepath = (path / "index.html")
    payload = part.get_payload(decode=True)
    text = payload.decode(part.get_content_charset() or "ASCII")
    path.mkdir(parents=True, exist_ok=True)
    # TODO: check filename validity
    filepath.write_text(payload)
    webbrowser.open(str(filepath))


def process_part(path, part):
    content_disposition = part.get("Content-Disposition")
    if content_disposition == "attachment":
        write_attachement(path, part)
    elif content_type == "text/plain":
        payload = part.get_payload(decode=True)
        text = payload.decode(part.get_content_charset() or "ASCII")
        print(text)
    elif content_type == "text/html":
        write_body(path, part)


def process_message(content):
    message = email.message_from_bytes(content)
    subject = decode(message["Subject"])
    from_address = decode(message["From"])
    print("Subject:", subject)
    print("From:", from_address)
    path = Path(clean(subject))
    for part in message.walk():
        process_part(path, part)
    print("="*100)


def main():
    with imaplib.IMAP4_SSL("outlook.office365.com") as imap:
        imap.login(USERNAME, PASSWORD)
        imap.select("INBOX")
        _, message_ids = imap.search(None, "ALL")
        message_ids = message_ids[0].split()
        for message_id in reversed(message_ids[-N:]):
            _, data = imap.fetch(message_id, "(RFC822)")
            process_mail(data[0][1])


if __name__ == "__main__":
    main()
Antworten