Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Email
email.qprovides a self-contained HTML email module. It supports two transports: the systemsendmailutility, or SMTP viacurl. HTML construction utilities are ported from qmail.Alert and report result handlers compatible with the TorQ reporter process are included, ported from
code/processes/reporter.q.Requirements
One of the following must be installed and configured:
smtpurlis configured. See Sendmail setup (msmtp) below.smtpurlis set in config. Most systems have this by default.Sendmail setup (msmtp)
msmtpis the recommended sendmail transport. It acts as a drop-insendmailreplacement and routes mail through an SMTP relay.1. Install
msmtp-mtacreates the/usr/sbin/sendmailsymlink that the module uses.2. Configure
Create
~/.msmtprc:Set permissions (msmtp refuses to run if the file is world-readable):
chmod 600 ~/.msmtprcFor Gmail,
passwordmust be an App Password — not your account password. App Passwords require 2-Step Verification to be enabled on the account.3. Test outside q
A successful send logs
exitcode=EX_OK. If it fails, the log contains the SMTP error.4. Test from q
Configuration
Passed as the first dictionary to
init. All keys are optional.mailfrom"torq@localhost"enabled0b1bto allow emails to be sentsmtpurl"""smtp://smtp.gmail.com:587". When set, curl is used instead of sendmailsmtpuser""smtppassword""smtpssl1b--ssl-reqd). Set0bto disableDependencies
Passed as the second dictionary to
init. Pass(::)to use all defaults.`log`info`warn`error, each{[c;m]}. Required —initthrows if absent. Seedi.logfor a default implementation.`send{[frm;to;sub;body;att]}— injectable send function. Pass(::)or omit to use curl smtp whensmtpurlis set, otherwise sendmail.Core Structures
history(table,.z.M) — Append-only log of everysenddefaultcall:time(timestamp),recipients(symbol),subject(any),status(symbol:`sent/`failed/`disabled),bytes(long:0jon success,-1jon failure or disabled)alertstats(keyed table,.z.M) — Tracks last alert send time perprocname+alertnamepair for cooldown enforcementMain Functions
initParameters:
[config; deps]Initialises the module. Pass
(::)for config to use defaults (email disabled, sendmail transport). Alogdependency is always required —initthrows if it is absent.When
smtpurlis set in config and no customsendis injected, the curl SMTP transport is used automatically.senddefaultParameters:
[msgdict]Sends an HTML email.
msgdictkeys:to— symbol or symbol list of recipientssubject— stringbody— list of strings (plain strings are wrapped in a styled<p>tag; pre-built HTML strings are passed through as-is; a timestamp footer is appended automatically)attachment— (optional) hsym file pathReturns
1bon success,0bon send failure,-1if disabled. Every attempt is logged tohistory.testParameters:
[to]Sends a test email to
to(symbol). Returns1bon success.alertParameters:
[period; recipients]Returns a result handler projection
{[data]}for the TorQ reporterresulthandlercolumn.period— timespan cooldown e.g.00:02:00recipients— string or list of strings (email addresses)data.resultmust have amessagescolumn (list of strings)reportParameters:
[temppath; recipients; filename; filetype]Returns a result handler projection
{[data]}for the TorQ reporterresulthandlercolumn. Writes result totemppath/filename.filetype, emails it as an attachment, then deletes the temp file.getstatusReturns the full
historytable.HTML Helpers
These functions are exported and can be used to build rich HTML email bodies before passing to
senddefault.addtext[text]<p>tagmailheading[level; text]<h1>–<h4>mailbold[text]mailitalic[text]mailtable[t]ztable[t]maildict[d]zdict[d]addcolor[color; text]mailbgcolor[hex; text]mailsize[px; text]mailcolors[color; bg; size; text]mailurl[url; text]Usage Examples
Every example requires a logger. Define one before calling
init:1. Send a plain email via sendmail
2. Send via SMTP
3. Send an HTML table
4. Test transport connectivity
5. Use as a reporter alert handler
6. Use as a reporter report handler
7. Testing with a mock send function