Created
February 21, 2025 02:47
-
-
Save levancho/6332dd5b898b48831a81aed3dc68d1fe to your computer and use it in GitHub Desktop.
processB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import psycopg2 | |
import requests | |
from exchangelib import Credentials, Account, Message, DELEGATE, HTMLBody | |
from email.mime.application import MIMEApplication | |
# PostgreSQL Database Configuration | |
DB_CONFIG = { | |
"dbname": "your_database", | |
"user": "your_db_user", | |
"password": "your_db_password", | |
"host": "your_db_host", | |
"port": "5432" | |
} | |
# Outlook Configuration | |
OUTLOOK_EMAIL = "[email protected]" # Your technical user email | |
OUTLOOK_PASSWORD = "your_password" # Your password | |
SHARED_MAILBOX = "[email protected]" # The shared mailbox | |
# API Configuration | |
PROCESS_API_URL = "https://your-api.com/processDocument" | |
# Folder where documents are stored | |
ATTACHMENT_DIR = "./attachments" | |
# Connect to PostgreSQL | |
conn = psycopg2.connect(**DB_CONFIG) | |
cursor = conn.cursor() | |
# Fetch record with status "Downloaded" | |
cursor.execute(""" | |
SELECT oid, senders, documents FROM email_records WHERE status = 'Downloaded' LIMIT 1 | |
""") | |
record = cursor.fetchone() | |
if record: | |
oid, senders, documents = record | |
# Split senders and documents | |
sender_list = senders.split(", ") | |
document_list = documents.split(", ") | |
# Prepare document files for API call | |
file_paths = [os.path.join(ATTACHMENT_DIR, doc.strip()) for doc in document_list if os.path.exists(os.path.join(ATTACHMENT_DIR, doc.strip()))] | |
if file_paths: | |
# Create a dictionary for files to send in the API request | |
files_to_send = [('files', (os.path.basename(f), open(f, 'rb'))) for f in file_paths] | |
# Call REST API once for all documents | |
response = requests.post(PROCESS_API_URL, files=files_to_send) | |
# Close opened file handles | |
for _, file in files_to_send: | |
file[1].close() | |
if response.status_code == 200: | |
html_content = response.text # Received HTML content | |
# Connect to Outlook | |
credentials = Credentials(OUTLOOK_EMAIL, OUTLOOK_PASSWORD) | |
account = Account( | |
primary_smtp_address=SHARED_MAILBOX, | |
credentials=credentials, | |
autodiscover=True, | |
access_type=DELEGATE | |
) | |
# Send email with processed document as an attachment | |
email = Message( | |
account=account, | |
subject="Processed Documents", | |
body=HTMLBody("<p>Attached is the processed document containing all input files.</p>"), | |
to_recipients=sender_list | |
) | |
html_attachment = MIMEApplication(html_content.encode('utf-8'), _subtype="html") | |
html_attachment.add_header('Content-Disposition', 'attachment', filename="Processed_Document.html") | |
email.attach(html_attachment) | |
email.send() | |
print(f"Email sent successfully to {', '.join(sender_list)}") | |
# Update database status | |
cursor.execute(""" | |
UPDATE email_records SET status = 'Processed' WHERE oid = %s | |
""", (oid,)) | |
conn.commit() | |
else: | |
print(f"Failed to process documents, API response: {response.status_code}") | |
else: | |
print("No valid documents found for processing.") | |
cursor.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment