Update displaysubnet.asp to match displaypc.asp style, add email API helper

- Rewrite displaysubnet.asp with two-column layout and profile card
- Add Details, Devices, and Edit tabs matching other display pages
- Use parameterized queries and HTML encoding for security
- Fix device queries to use machines/communications tables
- Add includes/email.asp helper for Python Email API integration
- Update api.asp GetShopfloorPCs to include all PC types with 10.134.* IPs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
cproudlock
2025-12-29 17:44:44 -05:00
parent 8a50f5c7b4
commit b0b300babd
3 changed files with 538 additions and 136 deletions

31
api.asp
View File

@@ -828,22 +828,27 @@ Sub GetDashboardData()
End Sub End Sub
Sub GetShopfloorPCs() Sub GetShopfloorPCs()
' Returns list of all active shopfloor PCs for remote management ' Returns list of all active PCs with shop floor IPs (10.134.*) for remote management
' This includes all PC types: Shopfloor, CMM, Wax Trace, Keyence, etc.
' PCs are identified by machinetypeid >= 33, pctypeid can be NULL
On Error Resume Next On Error Resume Next
Dim rsPC, strSQL, pcList, pcCount, pcData Dim rsPC, strSQL, pcList, pcCount, pcData
' Query active shopfloor PCs only (pctype = 'Shopfloor') ' Query all active PCs with shop floor IP addresses (10.134.*)
' Include hostname, machineid, machinenumber (equipment), IP address, last updated ' - machinetypeid >= 33 ensures we only get PCs (not equipment)
' - LEFT JOIN pctype to include PCs with NULL pctypeid
' - EXISTS subquery finds any PC with a 10.134.* address
strSQL = "SELECT m.machineid, m.hostname, m.machinenumber, m.serialnumber, " & _ strSQL = "SELECT m.machineid, m.hostname, m.machinenumber, m.serialnumber, " & _
"m.loggedinuser, m.lastupdated, " & _ "m.loggedinuser, m.lastupdated, " & _
"c.address AS ipaddress, " & _ "c.address AS ipaddress, " & _
"pt.typename AS pctype " & _ "COALESCE(pt.typename, 'Uncategorized') AS pctype " & _
"FROM machines m " & _ "FROM machines m " & _
"LEFT JOIN communications c ON m.machineid = c.machineid AND c.isprimary = 1 AND c.comstypeid = 1 " & _ "LEFT JOIN communications c ON m.machineid = c.machineid AND c.isprimary = 1 AND c.comstypeid = 1 " & _
"INNER JOIN pctype pt ON m.pctypeid = pt.pctypeid " & _ "LEFT JOIN pctype pt ON m.pctypeid = pt.pctypeid " & _
"WHERE m.isactive = 1 " & _ "WHERE m.isactive = 1 " & _
"AND pt.typename = 'Shopfloor' " & _ "AND m.machinetypeid >= 33 " & _
"AND EXISTS (SELECT 1 FROM communications c2 WHERE c2.machineid = m.machineid AND c2.address LIKE '10.134.%') " & _
"ORDER BY m.hostname ASC" "ORDER BY m.hostname ASC"
Set rsPC = objConn.Execute(strSQL) Set rsPC = objConn.Execute(strSQL)
@@ -1300,15 +1305,13 @@ Function InsertNetworkInterfaces(machineid, networkInterfacesJSON)
If interfaceName = "" Then interfaceName = "Interface " & (i + 1) If interfaceName = "" Then interfaceName = "Interface " & (i + 1)
' Determine if primary - 10.134.*.* is always primary for shopfloor PCs ' Determine if primary - 10.134.*.* is ALWAYS primary for shopfloor PCs
Dim isPrimary, isPrimaryFromJson ' Ignore JSON value, enforce by IP address pattern
Dim isPrimary
If Left(ipAddress, 7) = "10.134." Then
isPrimary = 1
Else
isPrimary = 0 isPrimary = 0
isPrimaryFromJson = GetJSONValue(interfacesArray(i), "IsPrimary")
If isPrimaryFromJson = True Or isPrimaryFromJson = "true" Or isPrimaryFromJson = "True" Then
isPrimary = 1
ElseIf Left(ipAddress, 7) = "10.134." Then
' Fallback: 10.134.*.* is always primary
isPrimary = 1
End If End If
' Insert into communications table ' Insert into communications table

View File

@@ -1,8 +1,17 @@
<!DOCTYPE html> <%
'=============================================================================
' FILE: displaysubnet.asp
' PURPOSE: Display detailed subnet information with edit capability
' SECURITY: Parameterized queries, HTML encoding, input validation
' UPDATED: 2025-12-29 - Migrated to match displaypc.asp style
'=============================================================================
%><!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<!--#include file="./includes/header.asp"--> <!--#include file="./includes/header.asp"-->
<!--#include file="./includes/sql.asp"--> <!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/validation.asp"-->
<!--#include file="./includes/db_helpers.asp"-->
</head> </head>
<% <%
@@ -11,45 +20,105 @@
theme = "bg-theme1" theme = "bg-theme1"
END IF END IF
search = Request.Querystring("search") '=============================================================================
' SMART REDIRECT: Check if search param is a printer or PC IP address
'----------------------------------------------------Is this the IP address of a printer??? ---------------------------------------------- '=============================================================================
Dim search
search = Trim(Request.QueryString("search") & "")
IF search <> "" THEN IF search <> "" THEN
strSQL = "Select printerid FROM printers where ipaddress='" &search &"'" ' Check if this IP belongs to a printer
set rs = objconn.Execute(strSQL) Dim rsPrinterCheck, strPrinterSQL
IF NOT rs.EOF THEN strPrinterSQL = "SELECT printerid FROM printers WHERE ipaddress = ?"
printerid = rs("printerid") Set rsPrinterCheck = ExecuteParameterizedQuery(objConn, strPrinterSQL, Array(search))
IF NOT rsPrinterCheck.EOF THEN
Dim printerRedirectId
printerRedirectId = rsPrinterCheck("printerid")
rsPrinterCheck.Close
Set rsPrinterCheck = Nothing
objConn.Close objConn.Close
Response.Redirect "./displayprinter.asp?printerid="&printerid Response.Redirect("./displayprinter.asp?printerid=" & printerRedirectId)
Response.End
END IF END IF
END IF rsPrinterCheck.Close
'-------------------------------------------------------Is this the IP address of a PC--------------------------------------------------- Set rsPrinterCheck = Nothing
IF search <> "" THEN
' PHASE 2: Query communications table instead of networkinterfaces ' Check if this IP belongs to a PC
strSQL = "SELECT c.machineid FROM communications c JOIN machines m ON c.machineid = m.machineid WHERE c.address='" &search &"' AND m.pctypeid IS NOT NULL LIMIT 1" Dim rsPCCheck, strPCSQL
set rs = objconn.Execute(strSQL) strPCSQL = "SELECT pcid FROM pc_network_interfaces WHERE ipaddress = ?"
IF NOT rs.EOF THEN Set rsPCCheck = ExecuteParameterizedQuery(objConn, strPCSQL, Array(search))
machineid = rs("machineid") IF NOT rsPCCheck.EOF THEN
Dim pcRedirectId
pcRedirectId = rsPCCheck("pcid")
rsPCCheck.Close
Set rsPCCheck = Nothing
objConn.Close objConn.Close
Response.Redirect "./displaypc.asp?machineid="&machineid Response.Redirect("./displaypc.asp?pcid=" & pcRedirectId)
Response.End
END IF END IF
rsPCCheck.Close
Set rsPCCheck = Nothing
END IF END IF
'----------------------------------------------------------------------------------------------------------------------------------------- '=============================================================================
' SECURITY: Validate subnet ID input
'=============================================================================
Dim subnetid
subnetid = GetSafeInteger("QS", "subnetid", 0, 1, 999999)
subnetid = Request.Querystring("subnetid") IF subnetid = 0 THEN
strSQL = "SELECT *,INET_NTOA(ipstart) AS subnetstart FROM subnets,subnettypes WHERE subnets.subnettypeid=subnettypes.subnettypeid AND subnets.isactive=1 AND subnetid="&subnetid objConn.Close
set rs = objconn.Execute(strSQL) Response.Redirect("displaysubnets.asp")
ipdiff = rs("ipend")-rs("ipstart") Response.End
'response.write(ipdiff) END IF
'=============================================================================
' SECURITY: Use parameterized query to prevent SQL injection
'=============================================================================
Dim strSQL, rs
strSQL = "SELECT subnets.*, subnettypes.subnettype, " & _
"INET_NTOA(subnets.ipstart) AS subnetstart, " & _
"INET_NTOA(subnets.ipend) AS subnetend " & _
"FROM subnets " & _
"LEFT JOIN subnettypes ON subnets.subnettypeid = subnettypes.subnettypeid " & _
"WHERE subnets.isactive = 1 AND subnets.subnetid = ?"
Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(subnetid))
' Check if subnet exists
IF rs.EOF THEN
objConn.Close
Response.Redirect("displaysubnets.asp")
Response.End
END IF
' Store values for use in page
Dim vlanVal, zoneVal, networkVal, networkEndVal, cidrVal, descVal, ipStartInt, ipEndInt
vlanVal = rs("vlan") & ""
zoneVal = rs("subnettype") & ""
networkVal = rs("subnetstart") & ""
networkEndVal = rs("subnetend") & ""
cidrVal = rs("cidr") & ""
descVal = rs("description") & ""
ipStartInt = rs("ipstart")
ipEndInt = rs("ipend")
If vlanVal = "" Then vlanVal = "N/A"
If zoneVal = "" Then zoneVal = "Unknown"
If networkVal = "" Then networkVal = "N/A"
If cidrVal = "" Then cidrVal = ""
If descVal = "" Then descVal = "No description"
' Calculate usable IPs
Dim usableIPs
If IsNumeric(ipStartInt) And IsNumeric(ipEndInt) Then
usableIPs = CLng(ipEndInt) - CLng(ipStartInt)
Else
usableIPs = 0
End If
%> %>
<body class="bg-theme <%=Server.HTMLEncode(theme)%>">
<body class="bg-theme <%Response.Write(theme)%>">
<!-- start loader --> <!-- start loader -->
<div id="pageloader-overlay" class="visible incoming"><div class="loader-wrapper-outer"><div class="loader-wrapper-inner"><div class="loader"></div></div></div></div> <div id="pageloader-overlay" class="visible incoming"><div class="loader-wrapper-outer"><div class="loader-wrapper-inner"><div class="loader"></div></div></div></div>
@@ -64,98 +133,240 @@
<div class="content-wrapper"> <div class="content-wrapper">
<div class="container-fluid"> <div class="container-fluid">
<div class="row mt-4">
<div class="col-lg-auto"> <div class="row mt-3">
<div class="col-lg-4">
<div class="card profile-card-2">
<div class="card-img-block">
<img class="img-fluid" src="./images/network/subnet-banner.png" alt="Subnet" onerror="this.src='./images/machines/default.png'">
</div>
<div class="card-body pt-5">
<img src="./images/network/subnet-icon.png" alt="subnet-icon" class="profile" onerror="this.src='./images/machines/default.png'">
<h5 class="card-title"><%=Server.HTMLEncode(networkVal)%><%=Server.HTMLEncode(cidrVal)%></h5>
<p class="card-text">VLAN <%=Server.HTMLEncode(vlanVal)%></p>
<p class="card-text text-muted"><%=Server.HTMLEncode(zoneVal)%></p>
</div>
</div>
</div>
<div class="col-lg-8">
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<ul class="nav nav-tabs nav-tabs-primary top-icon nav-justified"> <ul class="nav nav-tabs nav-tabs-primary top-icon nav-justified">
<li class="nav-item"> <li class="nav-item">
<a href="javascript:void();" data-target="#edit" data-toggle="pill" class="nav-link"><i class="zmdi zmdi-edit"></i> <span class="hidden-xs">Edit Subnet</span></a> <a href="javascript:void();" data-target="#profile" data-toggle="pill" class="nav-link active"><i class="icon-wrench"></i> <span class="hidden-xs">Details</span></a>
</li>
<li class="nav-item">
<a href="javascript:void();" data-target="#devices" data-toggle="pill" class="nav-link"><i class="zmdi zmdi-devices"></i> <span class="hidden-xs">Devices</span></a>
</li>
<li class="nav-item">
<a href="javascript:void();" data-target="#edit" data-toggle="pill" class="nav-link"><i class="icon-note"></i> <span class="hidden-xs">Edit</span></a>
</li> </li>
</ul> </ul>
<div class="tab-content p-3"> <div class="tab-content p-3">
<div class="tab-pane" id="edit"> <div class="tab-pane active" id="profile">
<h5 class="mb-3">Subnet Configuration</h5>
<div class="row">
<div class="col-md-3">
<p class="mb-2"><strong>VLAN:</strong></p>
<p class="mb-2"><strong>Zone:</strong></p>
<p class="mb-2"><strong>Network:</strong></p>
<p class="mb-2"><strong>CIDR:</strong></p>
<p class="mb-2"><strong>IP Range:</strong></p>
<p class="mb-2"><strong>Usable IPs:</strong></p>
<p class="mb-2"><strong>Description:</strong></p>
</div>
<div class="col-md-9">
<p class="mb-2"><%=Server.HTMLEncode(vlanVal)%></p>
<p class="mb-2"><span class="badge badge-info"><%=Server.HTMLEncode(zoneVal)%></span></p>
<p class="mb-2"><%=Server.HTMLEncode(networkVal)%></p>
<p class="mb-2"><%=Server.HTMLEncode(cidrVal)%></p>
<p class="mb-2"><%=Server.HTMLEncode(networkVal)%> - <%=Server.HTMLEncode(networkEndVal)%></p>
<p class="mb-2"><%=Server.HTMLEncode(CStr(usableIPs))%></p>
<p class="mb-2"><%=Server.HTMLEncode(descVal)%></p>
</div>
</div>
</div>
<div class="tab-pane" id="devices">
<h5 class="mb-3">Devices on this Subnet</h5>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-hover"> <table class="table table-hover table-striped">
<thead> <thead>
<tr> <tr>
<th scope="col">Vlan #</th> <th>Type</th>
<th scope="col">Zone</th> <th>Name</th>
<th scope="col">Network</th> <th>IP Address</th>
<th scope="col">CIDR</th>
<th scope="col">Description</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr>
<form method="post" action="./updatesubnetdirect.asp?subnetid=<%Response.Write(subnetid)%>">
<th scope="row"><input class="form-control" type="text" name="vlan" size="4" value="<%Response.Write(rs("vlan"))%>"></th>
<td><select name="subnettypeid" class="btn btn-light px-3">
<option value="<%Response.Write(rs("subnettypeid"))%>"><%Response.Write(rs("subnettype"))%></option>
<% <%
strSQL2 = "Select * FROM subnettypes where isactive=1 ORDER BY subnettype ASC" ' Get PCs on this subnet using machines/communications tables
set rs2 = objconn.Execute(strSQL2) Dim strSQL2, rs2, deviceCount
while not rs2.eof deviceCount = 0
Response.Write("<option class='btn' value='"&rs2("subnettypeid")&"'>"&rs2("subnettype")&"</option>")
rs2.movenext ' Query for PCs (machines with pctypeid IS NOT NULL)
wend strSQL2 = "SELECT m.machineid, m.hostname, c.address " & _
"FROM machines m " & _
"INNER JOIN communications c ON m.machineid = c.machineid AND c.isprimary = 1 " & _
"WHERE m.isactive = 1 AND m.pctypeid IS NOT NULL " & _
"AND INET_ATON(c.address) >= " & CLng(ipStartInt) & " " & _
"AND INET_ATON(c.address) <= " & CLng(ipEndInt) & " " & _
"ORDER BY INET_ATON(c.address)"
Set rs2 = objConn.Execute(strSQL2)
Do While Not rs2.EOF
deviceCount = deviceCount + 1
Dim pcDeviceName, pcDeviceIP, pcDeviceId
pcDeviceName = rs2("hostname") & ""
pcDeviceIP = rs2("address") & ""
pcDeviceId = rs2("machineid")
If pcDeviceName = "" Then pcDeviceName = "Unknown"
%> %>
</select> <tr>
</td> <td><span class="badge badge-primary">PC</span></td>
<td><input class="form-control" type="text" name="ipstart" size="24" value="<%Response.Write(rs("subnetstart"))%>"></td> <td><a href="displaypc.asp?machineid=<%=pcDeviceId%>"><%=Server.HTMLEncode(pcDeviceName)%></a></td>
<td><select name="cidr" class="btn btn-light px-3"> <td><%=Server.HTMLEncode(pcDeviceIP)%></td>
<option value="<%Response.Write(rs("cidr"))%>,<%Response.Write(ipdiff)%>"><%Response.Write(rs("cidr"))%></option>
<option value="/30,3">/30</option>
<option value="/29,7">/29</option>
<option value="/28,15">/28</option>
<option value="/27,31">/27</option>
<option value="/26,63">/26</option>
<option value="/25,127">/25</option>
<option value="/24,253">/24</option>
<option value="/23,511">/23</option>
<option value="/22,1023">/22</option>
<option value="/21,2047">/21</option>
<option value="/20,4095">/20</option>
</select>
</td>
<td><input class="form-control" type="text" name="description" size="40" value="<%Response.Write(rs("description"))%>"></td>
</tr> </tr>
<%
rs2.MoveNext
Loop
rs2.Close
Set rs2 = Nothing
' Query for Printers
Dim strSQL3, rs3
strSQL3 = "SELECT printerid, printerwindowsname, ipaddress " & _
"FROM printers " & _
"WHERE isactive = 1 " & _
"AND INET_ATON(ipaddress) >= " & CLng(ipStartInt) & " " & _
"AND INET_ATON(ipaddress) <= " & CLng(ipEndInt) & " " & _
"ORDER BY INET_ATON(ipaddress)"
Set rs3 = objConn.Execute(strSQL3)
Do While Not rs3.EOF
deviceCount = deviceCount + 1
Dim prtName, prtIP, prtId
prtName = rs3("printerwindowsname") & ""
prtIP = rs3("ipaddress") & ""
prtId = rs3("printerid")
If prtName = "" Then prtName = "Unknown Printer"
%>
<tr>
<td><span class="badge badge-success">Printer</span></td>
<td><a href="displayprinter.asp?printerid=<%=prtId%>"><%=Server.HTMLEncode(prtName)%></a></td>
<td><%=Server.HTMLEncode(prtIP)%></td>
</tr>
<%
rs3.MoveNext
Loop
rs3.Close
Set rs3 = Nothing
If deviceCount = 0 Then
%>
<tr>
<td colspan="3" class="text-muted text-center">No devices found on this subnet</td>
</tr>
<%
End If
%>
</tbody> </tbody>
</table> </table>
<div class="col-lg-4">
<BR>
<input type="submit" class="btn btn-primary" value="Update Subnet">
</div> </div>
<BR> <p class="text-muted mt-2"><small>Total devices: <%=deviceCount%></small></p>
</div>
<div class="tab-pane" id="edit">
<form method="post" action="./updatesubnetdirect.asp?subnetid=<%=subnetid%>" id="subnetEditForm">
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">VLAN #:</label>
<div class="col-lg-9">
<input type="text" class="form-control" name="vlan" value="<%=Server.HTMLEncode(rs("vlan") & "")%>" maxlength="10">
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Zone:</label>
<div class="col-lg-9">
<select name="subnettypeid" class="form-control">
<option value="<%=Server.HTMLEncode(rs("subnettypeid") & "")%>"><%=Server.HTMLEncode(zoneVal)%></option>
<%
Dim rsTypes, strTypesSQL
strTypesSQL = "SELECT * FROM subnettypes WHERE isactive = 1 ORDER BY subnettype ASC"
Set rsTypes = objConn.Execute(strTypesSQL)
Do While Not rsTypes.EOF
If CStr(rsTypes("subnettypeid") & "") <> CStr(rs("subnettypeid") & "") Then
Response.Write("<option value='" & Server.HTMLEncode(rsTypes("subnettypeid") & "") & "'>" & Server.HTMLEncode(rsTypes("subnettype") & "") & "</option>")
End If
rsTypes.MoveNext
Loop
rsTypes.Close
Set rsTypes = Nothing
%>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Network:</label>
<div class="col-lg-9">
<input type="text" class="form-control" name="ipstart" value="<%=Server.HTMLEncode(networkVal)%>" placeholder="e.g., 192.168.1.0">
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">CIDR:</label>
<div class="col-lg-9">
<select name="cidr" class="form-control">
<option value="<%=Server.HTMLEncode(cidrVal)%>,<%=usableIPs%>"><%=Server.HTMLEncode(cidrVal)%></option>
<option value="/30,3">/30 (4 IPs)</option>
<option value="/29,7">/29 (8 IPs)</option>
<option value="/28,15">/28 (16 IPs)</option>
<option value="/27,31">/27 (32 IPs)</option>
<option value="/26,63">/26 (64 IPs)</option>
<option value="/25,127">/25 (128 IPs)</option>
<option value="/24,253">/24 (256 IPs)</option>
<option value="/23,511">/23 (512 IPs)</option>
<option value="/22,1023">/22 (1024 IPs)</option>
<option value="/21,2047">/21 (2048 IPs)</option>
<option value="/20,4095">/20 (4096 IPs)</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Description:</label>
<div class="col-lg-9">
<input type="text" class="form-control" name="description" value="<%=Server.HTMLEncode(rs("description") & "")%>" maxlength="255">
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label"></label>
<div class="col-lg-9">
<input type="reset" class="btn btn-secondary" value="Cancel">
<input type="submit" class="btn btn-primary" value="Save Changes">
</div>
</div> </div>
</form> </form>
</div> </div>
<h5 class="card-title">Subnet Details</h5>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Vlan #</th>
<th scope="col">Zone</th>
<th scope="col">Network</th>
<th scope="col">CIDR</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><%Response.Write(rs("vlan"))%></th>
<td><%Response.Write(rs("subnettype"))%> </td>
<td><%Response.Write(rs("subnetstart"))%></td>
<td><%Response.Write(rs("cidr"))%></td>
<td><%Response.Write(rs("description"))%></td>
</tr>
</tbody>
</table>
</div> </div>
</div> </div>
</div> </div>
</div> <!-- End container-fluid--> </div>
</div>
<!--start overlay-->
<div class="overlay toggle-menu"></div>
<!--end overlay-->
</div>
<!-- End container-fluid-->
</div><!--End content-wrapper--> </div><!--End content-wrapper-->
<!--Start Back To Top Button--> <!--Start Back To Top Button-->
<a href="javaScript:void();" class="back-to-top"><i class="fa fa-angle-double-up"></i> </a> <a href="javaScript:void();" class="back-to-top"><i class="fa fa-angle-double-up"></i> </a>
@@ -185,6 +396,23 @@
<!-- Custom scripts --> <!-- Custom scripts -->
<script src="assets/js/app-script.js"></script> <script src="assets/js/app-script.js"></script>
<style>
.content-wrapper {
padding-bottom: 80px;
}
.footer {
position: relative !important;
bottom: auto !important;
}
</style>
</body> </body>
</html> </html>
<% objConn.Close %> <%
'=============================================================================
' CLEANUP
'=============================================================================
rs.Close
Set rs = Nothing
objConn.Close
%>

171
includes/email.asp Normal file
View File

@@ -0,0 +1,171 @@
<%
'=============================================================================
' 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
%>