<%
'=============================================================================
' FILE: includes/email.asp
' PURPOSE: Helper functions for sending emails via the Python Email API
' USAGE: Include this file, then call SendEmail() or SendEmailToGroup()
'
' REQUIREMENTS: Python Email API must be running on EMAIL_API_URL
'=============================================================================

' Configuration - adjust if API runs on different host/port
Const EMAIL_API_URL = "http://localhost:5000"
Const EMAIL_API_KEY = ""  ' Set if API requires authentication

'-----------------------------------------------------------------------------
' SendEmail - Send an email via the Email API
'
' Parameters:
'   toAddress  - Email address or comma-separated list
'   subject    - Email subject
'   message    - Email body (plain text or HTML)
'   isHtml     - True if message is HTML (optional, default False)
'
' Returns: True on success, False on failure
'-----------------------------------------------------------------------------
Function SendEmail(toAddress, subject, message, isHtml)
    On Error Resume Next

    Dim http, url, jsonBody, response

    ' Build JSON body
    jsonBody = "{" & _
        """to"": """ & EscapeJson(toAddress) & """," & _
        """subject"": """ & EscapeJson(subject) & """," & _
        """message"": """ & EscapeJson(message) & """," & _
        """html"": " & LCase(CStr(isHtml)) & _
    "}"

    url = EMAIL_API_URL & "/api/send"

    Set http = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
    http.setTimeouts 5000, 5000, 10000, 30000  ' resolve, connect, send, receive
    http.open "POST", url, False
    http.setRequestHeader "Content-Type", "application/json"

    If EMAIL_API_KEY <> "" Then
        http.setRequestHeader "X-API-Key", EMAIL_API_KEY
    End If

    http.send jsonBody

    If Err.Number <> 0 Then
        SendEmail = False
        Exit Function
    End If

    ' Check response
    If http.status = 200 Then
        SendEmail = True
    Else
        SendEmail = False
    End If

    Set http = Nothing
End Function

'-----------------------------------------------------------------------------
' SendEmailToGroup - Send email to a distribution group
'
' Parameters:
'   groupId    - Distribution group ID from database
'   subject    - Email subject
'   message    - Email body
'   isHtml     - True if message is HTML (optional)
'
' Returns: True on success, False on failure
'-----------------------------------------------------------------------------
Function SendEmailToGroup(groupId, subject, message, isHtml)
    On Error Resume Next

    Dim http, url, jsonBody

    jsonBody = "{" & _
        """group_id"": " & CInt(groupId) & "," & _
        """subject"": """ & EscapeJson(subject) & """," & _
        """message"": """ & EscapeJson(message) & """," & _
        """html"": " & LCase(CStr(isHtml)) & _
    "}"

    url = EMAIL_API_URL & "/api/send-to-group"

    Set http = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
    http.setTimeouts 5000, 5000, 10000, 30000
    http.open "POST", url, False
    http.setRequestHeader "Content-Type", "application/json"

    If EMAIL_API_KEY <> "" Then
        http.setRequestHeader "X-API-Key", EMAIL_API_KEY
    End If

    http.send jsonBody

    If Err.Number <> 0 Then
        SendEmailToGroup = False
        Exit Function
    End If

    If http.status = 200 Then
        SendEmailToGroup = True
    Else
        SendEmailToGroup = False
    End If

    Set http = Nothing
End Function

'-----------------------------------------------------------------------------
' SendEmailToGroupByName - Send email to a distribution group by name
'-----------------------------------------------------------------------------
Function SendEmailToGroupByName(groupName, subject, message, isHtml)
    On Error Resume Next

    Dim http, url, jsonBody

    jsonBody = "{" & _
        """group_name"": """ & EscapeJson(groupName) & """," & _
        """subject"": """ & EscapeJson(subject) & """," & _
        """message"": """ & EscapeJson(message) & """," & _
        """html"": " & LCase(CStr(isHtml)) & _
    "}"

    url = EMAIL_API_URL & "/api/send-to-group"

    Set http = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
    http.setTimeouts 5000, 5000, 10000, 30000
    http.open "POST", url, False
    http.setRequestHeader "Content-Type", "application/json"

    If EMAIL_API_KEY <> "" Then
        http.setRequestHeader "X-API-Key", EMAIL_API_KEY
    End If

    http.send jsonBody

    If Err.Number <> 0 Then
        SendEmailToGroupByName = False
        Exit Function
    End If

    If http.status = 200 Then
        SendEmailToGroupByName = True
    Else
        SendEmailToGroupByName = False
    End If

    Set http = Nothing
End Function

'-----------------------------------------------------------------------------
' EscapeJson - Escape special characters for JSON
'-----------------------------------------------------------------------------
Function EscapeJson(str)
    Dim result
    result = str
    result = Replace(result, "\", "\\")
    result = Replace(result, """", "\""")
    result = Replace(result, vbCr, "\r")
    result = Replace(result, vbLf, "\n")
    result = Replace(result, vbTab, "\t")
    EscapeJson = result
End Function
%>
