Getting attachment download from gmail

Hi @Allan_Zimmermann , @kowts I want to read a mail from gmail of specific subject and download the attachment from that mail to a specific path. i dont want to use Get mails activity which uses outlook mail. is there any way i can do this without using the get mails activiry

There is proberly some nuget packages that will allow that.
But personally I would use the “mail in” node in nodered or create an agent to handle that.
If some of the emails then need to be processed from openrpa, you can send it using a workitem or amqp message

and if you are lucky, @kowts has a python script that works in invoke code, that can do it :smiley:

@kowts please help on this.

Hi @Shivendra_Pratap and @Allan_Zimmermann

Yes I have one :grin:, you can achieve this using the imaplib library in Python to access your Gmail account and retrieve emails based on specific criteria like subject. Before you start, make sure to enable IMAP access for your Gmail account.
The imaplib library is actually a built-in Python library and doesn’t need to be installed separately. It provides functionality for interacting with IMAP mail servers. You don’t need to install it using pip.

Here’s a basic Python script that connects to your Gmail account, searches for emails with a specific subject, and downloads attachments from those emails:

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

# Gmail IMAP server and credentials
imap_server = 'imap.gmail.com'
email_address = 'your_email@gmail.com'
password = 'your_password'

# Connect to the mailbox
mail = imaplib.IMAP4_SSL(imap_server)
mail.login(email_address, password)
mail.select("inbox")  # Select the mailbox you want to search in

# Search for emails with a specific subject
target_subject = "Your Target Subject"
result, data = mail.search(None, f'(SUBJECT "{target_subject}")')

for num in data[0].split():
    # Fetch the email by its ID
    result, msg_data = mail.fetch(num, "(RFC822)")
    raw_email = msg_data[0][1]

    # Parse the email content
    email_message = email.message_from_bytes(raw_email)
    subject, _ = decode_header(email_message["Subject"])[0]

    # Print email content
    print(f"Subject: {subject}")
    print(f"From: {email_message['From']}")
    print(f"To: {email_message['To']}")
    print(f"Date: {email_message['Date']}")

    # Check for attachments
    for part in email_message.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue
        
        filename = part.get_filename()
        if filename:
            filepath = os.path.join("your_specific_path", filename)
            with open(filepath, 'wb') as f:
                f.write(part.get_payload(decode=True))
            print(f"Downloaded attachment: {filename}")

    # Print email body
    print("Email Body:")
    if email_message.is_multipart():
        for part in email_message.walk():
            content_type = part.get_content_type()
            if content_type == "text/plain":
                email_body = part.get_payload(decode=True).decode()
                print(email_body)
    else:
        email_body = email_message.get_payload(decode=True).decode()
        print(email_body)

    print("=" * 30)

# Logout from the mailbox
mail.logout()

But I prefere this methot, good ideia, NODE-RED is a powerfull tool.

Hope it help!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.