liorkesos / uzibot

0 stars 1 forks source link

mail parser #2

Open liorkesos opened 1 year ago

liorkesos commented 1 year ago

Get file by mail attachment and store it in customer storage

liorkesos commented 1 year ago

chatgpt says start from here...

import boto3

# Set up the Amazon S3 client
s3 = boto3.client('s3')

# Define the bucket where the attachment will be saved
bucket_name = 'my-bucket-name'

# Read the email using the email library
import email
msg = email.message_from_string('Your email message goes here')

# Find the attachment in the email
for part in msg.walk():
    if part.get_content_maintype() == 'multipart':
        continue
    if part.get('Content-Disposition') is None:
        continue

    # Save the attachment to the bucket
    file_name = part.get_filename()
    s3.upload_fileobj(part.get_payload(decode=True), bucket_name, file_name)