HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall/buyercall/blueprints/email/utils/parse_inbound_mail.py
import json


class InboundMailParser:
    @staticmethod
    def parse(data):
        try:
            data = json.loads(data) or {}
            message = json.loads(data.get('Message', '{}'))
            email_type = message.get('eventType', None)
            mail_content = message['mail']
            message_id = mail_content['messageId']
            from_email = mail_content.get('source')
            recipients = mail_content.get('destination')
            subject = None
            for h in mail_content['headers']:
                if h['name'] == 'Subject':
                    subject = h['value']
            body = message.get('content', None)

            return {
                "from_email": from_email,
                "recipients": recipients,
                "subject": subject,
                "body": body,
                "status": email_type,
                "message_id": message_id
            }
        except Exception as e:
            print("Error: ", e)
            return {}