Custom email send
Sending Data as Text or Attachments
This guide walks through how to collect data, export it as a CSV, email it as an attachment, and then clean up the file automatically.
We'll use the example of gathering current room temperature values and emailing them daily.
Overview of the Process
Create a job to run a query and generate the CSV file.
Create a second job to email the generated CSV to recipients.
Create a third job to delete the CSV file after the email is sent.
Each job should be scheduled so they run in the correct order and with a short buffer between them.
1. Create the CSV File
Query Explanation
We want to retrieve all points named "Room Temp" along with their current value and references (site, floor, equipment). The extra references help distinguish identical point names.
Breakdown of the query:
readAll(navName=="Room Temp")
Returns all points whosenavNameis Room Temp..keepCols([...])
Limits the output to the fields we care about:navName,curVal,siteRef,floorRef,equipRef..reorderCols([...])
Ensures the columns appear in a specific order instead of randomly..ioWriteCsv(io/My Temps.csv)
Writes the data to a CSV file in the io directory.
You may rename the file as needed.
Full Query
readAll(navName=="Room Temp")
.keepCols(["navName", "curVal", "floorRef", "equipRef", "siteRef"])
.reorderCols(["navName","curVal", "siteRef", "floorRef", "equipRef"])
.ioWriteCsv(`io/My Temps.csv`)Create the CSV Job
Go to DB Builder
Open Jobs Tree
Click Add
Enter a job name
Paste the full query into the Function field
Set a schedule (make sure it runs before the email job)
Save
2. Email the CSV Attachment
Using emailSend()
The function format is:
emailSend(recipients, subject, content, attachments)Parameters:
recipients
Single email:
"email@example.com"Multiple emails:
["email1@example.com","email2@example.com"]
subject
email body text
attachment path (optional)
Full Query Example
emailSend(
"rickyv@j2inn.com",
"My Temps",
"Attached you can find the Room Temps.",
`io/My Temps.csv`
)Create the Email Job
Go to DB Builder
Open Jobs Tree
Click Add
Enter a job name
Paste the email query into the Function field
Set a schedule (ensure it runs after the CSV job, but before the cleanup job)
Save
3. Delete the CSV File
To prevent build-up of files in the io directory, create a cleanup job.
Full Query
ioDelete(`io/My Temps.csv`)Create the Delete Job
Go to DB Builder
Open Jobs Tree
Click Add
Enter a job name
Paste the delete query into the Function field
Set the schedule (ensure it runs after the email job)
Save