Jacobvu84 / serenity-pageobject-junit-webdriver

4 stars 1 forks source link

Download Attach file trong gmail #91

Open Jacobvu84 opened 3 years ago

Jacobvu84 commented 3 years ago
import os
from imbox import Imbox  # pip install imbox
import traceback

# enable less secure apps on your google account
# https://myaccount.google.com/lesssecureapps

host = "imap.gmail.com"
username = "OneMountGroup@gmail.com"
password = 'OneMountGroup'
download_folder = "/home/OneMountGroup/"

if not os.path.isdir(download_folder):
    os.makedirs(download_folder, exist_ok=True)

mail = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False)
messages = mail.messages()  # defaults to inbox

for (uid, message) in messages:
    mail.mark_seen(uid)  # optional, mark message as read

    for idx, attachment in enumerate(message.attachments):
        try:
            att_fn = attachment.get('filename')
            download_path = f"{download_folder}/{att_fn}"
            print(download_path)
            with open(download_path, "wb") as fp:
                fp.write(attachment.get('content').read())
        except:
            print(traceback.print_exc())

mail.logout()

"""
Available Message filters: 

# Gets all messages from the inbox
messages = mail.messages()

# Unread messages
messages = mail.messages(unread=True)

# Flagged messages
messages = mail.messages(flagged=True)

# Un-flagged messages
messages = mail.messages(unflagged=True)

# Flagged messages
messages = mail.messages(flagged=True)

# Un-flagged messages
messages = mail.messages(unflagged=True)

# Messages sent FROM
messages = mail.messages(sent_from='sender@example.org')

# Messages sent TO
messages = mail.messages(sent_to='receiver@example.org')

# Messages received before specific date
messages = mail.messages(date__lt=datetime.date(2018, 7, 31))

# Messages received after specific date
messages = mail.messages(date__gt=datetime.date(2018, 7, 30))

# Messages received on a specific date
messages = mail.messages(date__on=datetime.date(2018, 7, 30))

# Messages whose subjects contain a string
messages = mail.messages(subject='Christmas')

# Messages from a specific folder
messages = mail.messages(folder='Social')
"""
Jacobvu84 commented 3 years ago
from imap_tools import MailBox, AND

# get email bodies from INBOX
with MailBox(host).login(username, password, 'INBOX') as mailbox:
    for msg in mailbox.fetch(AND(from_='jacob.vu@gmail.com')):
        body = msg.text or msg.html
        print(body)
Jacobvu84 commented 3 years ago

Tham khảo

Jacobvu84 commented 3 years ago

for uid, message in messages:
    # Every message is an object with the following keys
    message.raw_email

    print(message.keys())
    # To check all available keys
Jacobvu84 commented 3 years ago
import re

text = "This is your e-mail body. It contains a link to <a href='http//www.google.com'>click here</a>."

link_pattern = re.compile('href=\'(.*?)[^>]*')
search = link_pattern.search(text)
if search is not None:
    print("Link found! -> " + search.group(0).replace("'","").replace("href=",""))

else:
    print("No links were found.")