README
¶
Mailcrier
Started as a fork of github.com/iggy/slendmail.
A sendmail replacement for machines that send no email: cron, at, sudo,
mdadm, smartd, fail2ban and every other tool that pipes a message into
/usr/sbin/sendmail get it delivered to Telegram, Discord, Slack, ntfy,
an HTTP webhook, a service of the shoutrrr library or a program of their
own instead.
- Accepts the command lines of the usual callers (
-t,-i,-f,-F,-oi,-bi,-qand the rest) and reads MIME messages: encoded words, quoted-printable, base64, any charset, HTML parts and attachments. - Sends to all configured targets at the same time, each in the markup of its service with everything from the message escaped, cut to the length the service accepts, with the attachments it can take.
- Keeps a message in a spool until every target has it: a service that is down gets it on a later queue run.
- One static binary, installed setgid so that the jobs of any user can send while only root reads the tokens; logs to syslog.
Build
go build -o mailcrier ./cmd/mailcrier
With -tags noshoutrrr the binary leaves out the shoutrrr library and
the 12 modules it brings, about 3 MB of 13.7 MB (linux/amd64, -trimpath -ldflags='-s -w', go1.27.1); a shoutrrr target then rejects the
configuration with exit status 78 and built without shoutrrr in the log.
Releases carry both builds, the smaller one as mailcrier-minimal.
The binary goes to /usr/sbin/mailcrier, with /usr/sbin/sendmail,
/usr/lib/sendmail, /usr/bin/newaliases and /usr/bin/mailq as links
to it; see "Options, privileges and containers" for ownership and modes.
The manual page is docs/mailcrier.8.
Install
Each release on https://github.com/6RUN0/mailcrier/releases has deb, rpm
and apk packages of the full build for amd64, arm64 and armv7, without
signatures (apk add --allow-untrusted), and checksums.txt with the
SHA-256 of every file. Every file listed there also has a build provenance
attestation of the release workflow, which the GitHub CLI checks:
sha256sum --ignore-missing -c checksums.txt
gh attestation verify --repo 6RUN0/mailcrier \
--signer-workflow 6RUN0/mailcrier/.github/workflows/release.yml \
./mailcrier_<version>_linux_amd64.deb
apt install ./mailcrier_<version>_linux_amd64.deb
dnf install ./mailcrier_<version>_linux_amd64.rpm
apk add --allow-untrusted ./mailcrier_<version>_linux_amd64.apk
| Path | Owner and mode |
|---|---|
/usr/sbin/mailcrier |
root:mailcrier 2755, set by the scripts after unpacking |
/usr/sbin/sendmail, /usr/lib/sendmail, /usr/bin/mailq, /usr/bin/newaliases |
links to it; in rpm the alternative mta |
/etc/mailcrier.conf |
root:mailcrier 0640, no target |
/etc/mailcrier.d/ |
root:mailcrier 0750, for token_file and the like |
/var/spool/mailcrier/ |
root:mailcrier 0750; tmp, queue, hold, failed, locks in it 2770 |
mailcrier-queue.timer and .service, /etc/cron.d/mailcrier |
queue run, deb and rpm |
/etc/crontabs/mailcrier |
queue run, apk |
/usr/share/doc/mailcrier/examples/ |
example configurations, see "Configuration" |
/usr/share/selinux/packages/mailcrier/mailcrier.cil |
SELinux module, rpm, see below |
The install creates the system group and user mailcrier (home
/var/spool/mailcrier, no login shell) unless they exist. Then, as root:
- Write a target into
/etc/mailcrier.conf; its comments and the examples show how. A secret goes alone into a file under/etc/mailcrier.d,root:mailcrier 0640. mailcrier --check-configreports errors and warnings, exit status 0 when the file loads.mailcrier --probesends a test message to every target.
On upgrade, rpm and apk set the owner and mode of /etc/mailcrier.conf
back to root:mailcrier 0640 when its content is that of the package;
dpkg keeps them. A secret therefore belongs in a file under
/etc/mailcrier.d/, where the package ships no file and changes none.
Until a target is set, every message is held in hold/ with exit status
78, and mailcrier-queue.service exits 78 every 5 minutes and is listed by
systemctl --failed; the first queue run after the configuration is fixed
delivers the held messages.
The timer is enabled on install and started where systemd runs; where it does not, the cron file runs the queue. The package scripts never run mailcrier and never touch the configuration.
- Debian and Ubuntu: the package provides, conflicts with and replaces
mail-transport-agent, so installing it removes the MTA in place (postfix, exim4, msmtp-mta, ...), and removing mailcrier does not bring that one back. The setgid bit is adpkg-statoverrideentry, which dpkg applies to every later version; one the administrator set before is kept. - RHEL, Rocky, Alma, Fedora: the links are the alternative
mtawith priority 100, above postfix (60) and sendmail (90), so mailcrier is the active one after install;alternatives --config mtachooses another. After removal the remaining MTA is active again. The package installs an SELinux module that labels mailcrier like a stock MTA, so confined callers such as smartd can mail; a hook then runs in the mail domain of its caller, see docs/selinux.md. cronie decides when it starts whether it mails the output of jobs: a crond started while no MTA was installed logs that output untilsystemctl restart crond, and the install prints a reminder while crond runs. - Alpine: the package replaces the BusyBox link
/usr/sbin/sendmail, and BusyBox puts it back after removal. ssmtp, dma and opensmtpd own/usr/sbin/sendmailas well: apk refuses to overwrite it (trying to overwrite usr/sbin/sendmail owned by ...), so remove them first. The queue runs from BusyBox crond, which Alpine does not start by default:rc-update add crond && rc-service crond start. Without it a message that failed is retried only by the next call of the same user.
In rpm and apk the binary is unpacked as root:root 0755 on install and on
every upgrade, and the install script then gives it the group and the
setgid bit. Consequences:
rpm -V mailcrierreports.M....G.. /usr/sbin/mailcrierandapk audit --check-permissionsreportsM usr/sbin/mailcrier; that is expected.- During an upgrade, between unpacking and the script, a call of a user
other than root cannot read the configuration: it exits 78 and the
message is lost, since
hold/is not writable for that user either. rpm --setperms,rpm --setugids,rpm --restore, an install or upgrade with--noscripts,apk fixandapk add --no-scriptsremove the bit for good, with the same result for every such call until it is restored as root:chgrp mailcrier /usr/sbin/mailcrier && chmod 2755 /usr/sbin/mailcrier(chgrpfirst: it clears the bit).
Removal (dpkg -r, rpm -e, apk del) stops the timer. It keeps the
user, the group and a changed configuration (dpkg -P deletes it, rpm -e
keeps it as /etc/mailcrier.conf.rpmsave). A spool without messages in
queue/, hold/ and failed/ is removed with the package; one with
messages stays, and every format prints its path on removal (deb on
dpkg -r and on dpkg -P). A call during the removal can leave the spool
with nothing but locks/ in it, and files an interrupted call left in
tmp/ keep it as well (dpkg reports the directory as not empty);
such a directory holds no message and can be deleted. Debian keeps
/etc/cron.d/mailcrier until dpkg -P; its job does nothing while the
binary is missing.
Configuration
The program reads /etc/mailcrier.conf, a TOML file. Every run parses the
file strictly: an unknown key, a key that the target type does not use, or
an invalid value rejects the file, the message is not delivered but held
in the spool (see "Spool and queue"), and the exit status is 78. A package
installs an example without targets, so until targets are configured every
message is held with status 78; that is expected.
Example configurations to copy whole are installed in
/usr/share/doc/mailcrier/examples/ and kept in
packaging/examples/:
| File | Shows |
|---|---|
mailcrier.conf |
every table with its defaults and one minimal target of each type, as comments like the installed file |
telegram.conf |
one Telegram chat with a forum topic, on_long and max_lines |
team-chat.conf |
Slack, Discord and Mattermost together |
ntfy.conf |
ntfy with the file limit of ntfy.sh and a template |
webhook.conf |
http targets with method, headers, path and query templates and generic-json |
hook.conf |
an exec hook with argv and timeout, the message on stdin |
routes.conf |
several targets, routes by recipient and subject, continue, a catch-all rule, [[suppress]] for a noisy cron job, telegram_direct |
templates.conf |
template and template_file with the template functions |
container.conf |
a container without the setgid bit: the spool on a volume, secrets in /run/secrets |
[general]
syslog_tag = "mailcrier" # optional, default "mailcrier"
http_timeout = "15s" # optional, one HTTP request
deadline = "30s" # optional, delivery to all targets
[strings] # optional, notices in place of missing content
no_subject = "(no subject)"
empty_body = "(empty body)"
truncated = "[truncated]"
truncated_size = "[truncated, %s in full]"
more_attachments = "... and %d more"
not_sent = "[not sent]"
[target.hook] # the table key is the target name: [a-z0-9-]
type = "http"
url_file = "/etc/mailcrier.d/hook.url" # or url = "https://..."
preset = "generic-json"
- Exactly one of
urlandurl_fileis set; likewisetokenandtoken_file. A*_filekey takes an absolute path; the file content, with surrounding whitespace removed, is used as the value. [strings]replaces the English notices that stand in for missing content:no_subjectfor a message without a subject,empty_bodyfor a body without visible text,truncatedat the end of a body cut to the length limit of a target,truncated_sizein its place when the full text does not go along as a file, with%sfor its size (exactly one%sand no other%, else exit status 78; when onlytruncatedis set, it stands there too, without the size),more_attachmentsafter a list of attachments cut to that limit, with%dfor the number left out (exactly one%dand no other%, else exit status 78),not_sentafter an attachment listed in the text but not sent because it exceeds a file limit of the target. Each is optional and must not be blank; the values above are the defaults. Thegeneric-jsonpreset uses none of them: it carries subject and body as they are, empty when the message has none, and has no length limit.- A text longer than the target accepts is cut: first a subject longer
than a quarter of the limit (at least 64 units of the target) is cut at
a word and ends with
..., then the list of attachments, then the body, which ends with thetruncatednotice. The limit is counted the way the service counts it, after escaping. When the rest of the text, host and sender included, is too long on its own, the text is cut hard; for Telegram between tags and entities, with the open tags closed. What goes along with a cut text is set per target, see "Long messages". - Exit status: see "Exit status" below.
- Logging goes to syslog, facility
mail. Where no syslog socket exists (a container without/dev/log) the records go to stderr, with time and level, after onesyslog unavailablewarning; PHP-FPM passes the stderr of a worker to its own log only withcatch_workers_output = yesin the pool configuration. A setgid-elevated process without syslog writes to stderr onlymailcrier: <message>for warnings and errors, without fields, because its caller must not see what only the group may read. Each record is a constant message with logfmt fields:call(16 hex digits, one value per invocation) on every record,msgid(the Message-ID header, cut to 256 bytes, absent when the message has none) andsize(bytes read) for the message,target,class(temporperm),status(the HTTP status, when the service answered) andretry_after(the delay the service asked for) for a failed target.hook output(info, warning when the run failed) carries what anexechook printed, see "Targets".text truncated for target(info) names a target that got a cut text,text rejected, sent as file(warning) one that refused the text and got it as a file,text rejected, file failed too(warning) one that took neither,attachments not delivered(warning) one that took the text but not the files; the message counts as delivered there.template failed, built-in used(warning) names a target whose template from the configuration failed,request template failed, message not sent(warning) anhttptarget whose path, query or header template failed, see "Templates". Records about a spool entry carry itsid. Headers and body of the message are never logged, nor is the response body of a service. - Tokens and URLs, including the content of
*_file, are replaced by***in every log record and in the debug output of the Go HTTP stack. A URL is masked whole, and so are its host, host labels, request URI, path segments and query values of 16 characters or more (the request URI also when it has a query) and its userinfo. http_timeoutbounds one HTTP request from dialing to the end of the response;deadlinebounds the delivery to all targets together. Both take Go duration strings ("500ms","15s","1m") and must be positive. A target that runs out of either counts as a temporary failure.- Proxy environment variables (
HTTPS_PROXY,HTTP_PROXY,NO_PROXY) are honoured without elevation and ignored by a setgid-elevated process. - The file mode creation mask is always
007.
Targets
Every target of the file receives every message unless routes or direct
chats choose the targets, see "Routes and suppression"; the targets of a
message are sent to at the same time. Each sends the text in the markup of its
service, escaped so that nothing in the message becomes markup, a link
preview or a mention; shoutrrr sends plain text, and exec hands on the
message itself instead.
max_file_size, a key of every type but http, exec and shoutrrr,
sets the size limit of one file in bytes, in place of the one given below
for the type (slack has none); it must be a positive integer, else exit
status 78. A file over it is listed with the not_sent notice and not
sent, and a cut text whose full text exceeds it ends with the
truncated_size notice. A value above the limit of the service is taken as
it is, and the service rejects a bigger file.
[target.ops-telegram]
type = "telegram"
token_file = "/etc/mailcrier.d/tg.token" # or token = "123456:ABC..."
chat_id = -1001234567890 # or "-1001234567890", "@channel"
message_thread_id = 42 # optional, forum topic
disable_notification = true # optional, silent messages
telegram:chat_id(an integer or a non-blank string) is required.sendMessagewithparse_modeHTML and link previews off, then onesendDocumentper attachment; a text of at most 1024 characters goes as the caption of the first document instead, unless that file is empty. The text holds at most 4096 characters as Telegram counts them, after entities; a character outside the BMP, such as an emoji, counts once. Up to 10 files of at most 50 MB each are sent, the others are listed with thenot_sentnotice. An answer with"ok": falseis a failure even with status 200;error_code429 and 5xx are temporary, withparameters.retry_afterlogged asretry_after. When the text arrived and a document did not, the message counts as delivered andattachments not deliveredis logged.
[target.ops-discord]
type = "discord"
url_file = "/etc/mailcrier.d/discord.url" # https://discord.com/api/webhooks/<id>/<token>
discord: one webhook execution per message withwait=true, so that Discord answers after storing the message, andallowed_mentionswith an emptyparselist, so that@everyone,@hereand user or role mentions in the message do not ping. A query of the URL, such asthread_id, is kept. The text holds at most 2000 characters (counted in UTF-16 units); up to 10 files of at most 20 MiB and 25 MiB together go in the same request. As for every target, 429, 5xx and any answer with aRetry-Afterheader are temporary, any other failure is permanent.
[target.prod-slack]
type = "slack"
token_file = "/etc/mailcrier.d/slack.token" # bot token, xoxb-...
channel = "#alerts" # channel name or ID
slack:channelis required and must not be blank; white space around it is dropped.chat.postMessageof the Web API with the bot token as a bearer token and link unfurling off, then per attachmentfiles.getUploadURLExternaland an upload, and onefiles.completeUploadExternalthat shares the files in the thread of the message; the bot needschat:writeandfiles:writeand must be a member of a private channel.&,<and>of the message are escaped, so<!channel>or<@U123>stays text. The text holds at most 40000 characters; up to 10 files are sent. An answer with"ok": falseis a failure;ratelimited,internal_error,fatal_error,service_unavailableandrequest_timeoutare temporary. Severalslacktargets may post with different tokens to different channels.
[target.phone]
type = "ntfy"
# https://ntfy.sh/<topic>, user:password@ allowed
url_file = "/etc/mailcrier.d/ntfy.url"
max_file_size = 2000000 # optional, ntfy.sh takes files of 2 MB
ntfy: a JSON publish to the server root with the topic, the subject as title (line breaks as spaces, at most 1 KB) and the text of at most 4096 bytes, then one PUT per attachment to the topic withfilenameandtitlein the query; up to 10 files of at most 15 MiB each, the defaults of an ntfy server. ntfy.sh takes files of at most 2 MB, so a target there setsmax_file_size. The URL must end with the topic, otherwise exit status 78; user information and a query such asauthare sent with every request.
[target.mm]
type = "http"
preset = "mattermost" # mattermost | slack-webhook | generic-json
url_file = "/etc/mailcrier.d/mm.url"
username = "mailcrier" # optional, mattermost only
channel = "alerts" # optional, mattermost only
[target.api]
type = "http"
preset = "generic-json"
url = "https://api.example.org/notify"
method = "PUT" # optional: POST (default), GET, PUT, PATCH
[target.api.headers] # optional, values are templates
Authorization = "Bearer api-token"
[target.ping]
type = "http"
method = "GET"
url = "https://status.example.org/api/push/abc"
path = "/{{ pathSegment .Hostname }}" # optional, after the path of url
[target.ping.query] # optional, values are templates
status = "down"
msg = "{{ .Subject | truncate 200 }}"
http: one request of the JSON document of its preset, or of its template (see "Templates"), withContent-Type: application/jsonand no files. Exactly one ofpreset,templateandtemplate_fileis set.methodisPOST, the default,GET,PUTorPATCH, in capitals; any other value exits 78. AGETsends no body and no Content-Type, so it takes none ofpreset,template,template_file,max_textandmax_lines; the message reaches the service throughpath,queryand the headers.slack-webhookis cut to 40000 characters, the length Slack keeps;mattermosthas no limit, since Mattermost splits a long text into several posts, nor hasgeneric-json.mattermostposts{"text": ...}with subject, host, sender and body in Markdown,@channel,@alland@heredisarmed, andusernameandchanneladded when set; a setchannelmust not be blank, white space around it is dropped.max_textandmax_lines(see "Long messages") apply to every preset.slack-webhookposts{"text": ...}in Slack mrkdwn with&,<and>escaped;generic-jsonposts{"subject": ..., "body": ..., "hostname": ...}.[target.<name>.headers]adds request headers after the Content-Type, so aContent-Typethere replaces it; a header name that is not an HTTP token, a value with a line break or NUL,Host,Content-Length,Transfer-EncodingorTrailer, which the HTTP client sets itself and would drop, andConnection,Keep-Alive,Proxy-ConnectionorUpgrade, which HTTP/2 does not carry, exit 78.pathis appended to the path ofurl, and[target.<name>.query]adds its values to the query ofurl, encoded, after the queryurlhas. The values ofheadersandqueryandpathare templates (see "Templates") rendered for every message; scheme, host and port come fromurlorurl_filealone. A path that renders empty appends nothing, and the request goes tourlas it is. A rendered path that does not start with a single/, holds?,#, a backslash, a control character or a.or..segment (escaped once or twice, or followed by a;parameter), or a rendered header value with a line break or NUL fails the target permanently before any request; an escaped/in the path stays escaped. Whatpathwrites before its first{{ }}action is checked by these rules when the configuration is loaded, exit status 78. A value from the message goes into a path segment throughpathSegment, which escapes/as well, but not dots: a value that is.or.., or holds/../, still fails the target for that message. The text of header and query values outside the{{ }}actions, 8 characters or more, and the credential after a scheme such asBearer, are masked in the log like tokens, and so is every piece of the text ofpathoutside the actions between two slashes of 16 characters or more, as written and unescaped. A redirect is not followed and counts as a permanent failure.
[target.run]
type = "exec"
argv = ["/usr/local/bin/notify", "--channel", "ops"]
timeout = "30s" # optional, the default
exec: runs a program once per message.argvis the command line, run without a shell;argv[0]must be an absolute path, and no element may hold a NUL, else exit status 78. Nothing of the message goes intoargv.- stdin: the message as it was read, without its Bcc and Resent-Bcc fields. The hook gets no text and no files, so the keys about text and files are not valid for it.
- Environment: only
PATH=/usr/sbin:/usr/bin:/sbin:/bin,LANG=C.UTF-8,TZwhen the caller set it (a setgid call keeps only a zone name), andMAILCRIER_SUBJECT(the decoded subject),MAILCRIER_FROM(the address of From, or the envelope sender),MAILCRIER_TO(the envelope recipients joined by,, without the addresses only Bcc or Resent-Bcc named),MAILCRIER_HOSTNAME,MAILCRIER_TARGET(the target name),MAILCRIER_MSGID(the Message-ID, empty when there is none) andMAILCRIER_SIZE(the bytes on stdin). In each value CR, LF and NUL become spaces, and the value is cut at a character to 4096 bytes; the whole environment stays within 64 KiB. The values come from whoever wrote the message: a hook quotes them ("$MAILCRIER_SUBJECT") and never hands them toevalorsh -c. - Outcome: exit status 0 is a delivery; 75 (
EX_TEMPFAIL) a temporary failure, queued and retried; any other status, or death by a signal, a permanent failure. A hook that cannot be started (missing file, no execute permission,E2BIG) fails permanently. - Time:
timeout, a positive Go duration, 30 s by default, bounds one run; so dodeadlineand the budget of a queue run, whichever ends first. Past it the process group of the hook (each hook starts in its own) getsSIGKILLand the run is a temporary failure; a child that left the group withsetsidsurvives. After the hook exits, the call waits at most 2 s for children that still hold its stdout or stderr, and then goes on without them.SIGINT,SIGTERMorSIGHUPto mailcrier end the run the same way: the group is killed, the failure is temporary and the message stays queued (a second signal ends mailcrier at once). This holds once the message is read; a signal while it is still coming in, such as Ctrl-C while it is typed, ends mailcrier and discards the message, as sendmail does. A signal that mailcrier was started with ignored, as undernohup, stays ignored. When mailcrier dies bySIGKILL, the kernel kills the hook itself (parent death signal), but children the hook started survive and finish on their own. - Output: stdout and stderr together, the first 4096 bytes, go into one
hook outputrecord withtarget,outputandoutput_size(all bytes printed), the secrets of the configuration masked. - Ids: a setgid call starts the hook with the real uid and gid of the
caller and the caller's supplementary groups, without the
mailcriergroup, so the hook reads neither the configuration nor the spool. A queue run as themailcrieruser (the systemd timer or the cron file) starts the hooks of queued messages asmailcrierwith the groupmailcrier, which reads the configuration with all tokens and every spool entry: the hook program must be trusted that far. The working directory is that of the caller. - Secrets:
argvis visible to every local user throughpsand/proc/<pid>/cmdlinewhile the hook runs, and mailcrier does not mask it in the log, so it holds no token. A hook that needs one reads it itself; started by a setgid call it runs as the caller, so the secret is readable by every user who can runsendmail. Where that is not acceptable, the hook has to refuse to run as anyone butmailcrier(exit 75 keeps the message queued for the queue run of that user), and then reads whatmailcrierreads, as said above.
[target.bus]
type = "shoutrrr"
url_file = "/etc/mailcrier.d/bus.url" # a service URL, gotify://host/token
shoutrrr: one message through a service of the shoutrrr library (github.com/nicholas-fedor/shoutrrr v0.21.1), chosen by the scheme of the URL:gotify,matrix,teams,pushover,smtp,genericand the others its documentation lists. The text is that of the plain template (subject, host, sender, body), without files and without a title; there is no length limit of mailcrier's, each service cuts or splits a long text as the library does. An unknown scheme or a URL the service rejects exits 78 when the configuration is loaded.matrixtakes an access token only (matrix://:token@host): with a user name the library logs in while the configuration is loaded, outsidehttp_timeoutanddeadline, so such a URL exits 78 without contacting the server. A failure after an HTTP answer outside 2xx is classified by its status, as for the other targets; a redirect is not followed and fails permanently even where the library would count it a success; any other failure is temporary and stays queued untilqueue_ttl. The services that send over HTTP usehttp_timeoutand the proxy rule above;smtp,xmppandmqttopen their own connections without a proxy, bounded only bydeadline. Error texts of the library may quote the URL, which is masked in the log as every URL is.
Long messages
[target.ops-telegram]
type = "telegram"
token_file = "/etc/mailcrier.d/tg.token"
chat_id = -1001234567890
on_long = "file" # optional: file | truncate | blockquote
long_file = "text" # optional: text (message.txt) | eml (message.eml)
max_text = 2000 # optional, below the limit of the service
max_lines = 40 # optional, lines of the body in the text
- A text over the limit of a target is cut as described under
"Configuration". With
on_long = "file", the default, a target that sends files gets the full text asmessage.txt(text/plain, UTF-8, the message in the plain layout without a limit) as its first file, listed first among the attachments of the text, which ends the cut body with thetruncatednotice.long_file = "eml"sends the message itself asmessage.emlinstead, as it was read, without itsBccandResent-Bccfields; a queued message is sent the same way. on_long = "truncate"sends the cut text alone; so does a target without files (http) and a target whose file limit the full text exceeds. The body then ends with thetruncated_sizenotice, such as[truncated, 1.9 MiB in full], with the size ofmessage.txt.on_long = "blockquote"isfilewith the cut body in an expandable blockquote in Telegram, which shows a few lines until opened; the other targets have no such block and treat it asfile.max_textreplaces the text limit of the service, in its unit: the characters of Telegram and of themattermostandgeneric-jsondocuments, the UTF-16 units of Discord, Slack and theslack-webhookdocument, the bytes of ntfy. A value above the limit of the service is taken as it is, and the service rejects a longer text. Forhttpthe limit covers the whole JSON document: one too small for the document without body and subject fails the target permanently.max_linescuts a body of more lines after that many and treats the text as cut; trailing empty lines do not count. Both must be positive;long_file, likeon_long, is not a key ofhttp.- A Telegram target that answers 400 to the text, for markup it cannot
parse or a text over its limit, gets the full text once more as a file
alone, without text or caption, whatever its
on_long; the log recordstext rejected, sent as file(warning). When that fails too, the record istext rejected, file failed too(warning) and the target fails with the error of the second attempt. Every 400 counts, a wrongchat_idincluded, which costs one more request.
Templates
[target.ops-telegram]
type = "telegram"
token_file = "/etc/mailcrier.d/tg.token"
chat_id = -1001234567890
template = '''
<b>{{ .Subject | default .Strings.NoSubject | tgHTML }}</b>
<i>{{ .Hostname | tgHTML }}</i> {{ .Date | tz "Europe/Berlin" | date "15:04" }}
<pre>{{ .Body | default .Strings.EmptyBody | trimEnd | tgHTML }}</pre>'''
[target.api]
type = "http"
url = "https://api.example.org/notify"
template = '''
{"title": {{ toJson .Subject }}, "host": {{ toJson .Hostname }},
"text": {{ toJson .Body }}}'''
-
template, a Gotext/template(https://pkg.go.dev/text/template), replaces the built-in template of a target of any type butexec;template_file, an absolute path, names a file that holds it, read when the configuration is loaded, without the line break at its end. Not both; a blank template or file exits 78. Anhttptarget takes exactly one ofpresetand a template, sousernameandchannelare not available with a template. -
The template writes the markup of the target: Telegram HTML (the Bot API parse mode is HTML, there is no other), Discord markdown, Slack mrkdwn, the plain text of ntfy (the title stays the subject) and of
shoutrrr, the JSON request body ofhttp, sent withContent-Type: application/jsonunless a header replaces it. Everything from the message comes from whoever wrote it and goes through the escaper of that markup:tgHTML;discordEscape,discordCodeinside a code block;slackEscape,slackCode;mmEscape,mmCodefor Mattermost markdown;toJsonfor a JSON value. -
Secrets stay out of the
{{ }}actions: an error quotes the action it failed in, and goes to the log. A token belongs in a header, in the URL, or in the template text outside the actions, which no error quotes. -
A template that does not parse, calls an unknown function or uses
define,templateorblockrejects the configuration: exit status 78, the message is held, andconfiguration rejectednames the target and the line in the template. -
The data:
Field Content .Subjectdecoded Subject, empty when the message has none .FromFrom address, else the envelope sender: .From.Name,.From.Addr,.From.String.Sender,.SenderNameenvelope sender and the name of -F.To,.Ccaddresses of the headers, like .From.Recipientsenvelope recipients without the addresses only Bcc or Resent-Bcc named .Date,.ReceivedAtthe Date header (the receive time without one) and the receive time .MessageIDthe Message-ID header .Headersall header fields but Bcc and Resent-Bcc: .Headers.Get "X-Cron-Env"(the first value),.Headers.Values(all),.Headers.Has,.Headers.Names, names in any case.Body,.BodyHTMLthe text, and the HTML part when there is one .Attachments,.MoreAttachments.Name,.ContentType,.Size,.IsSkipped(listed, not sent) of each attachment listed, and the number left out.Hostname,.Target,.Limithost name, target name, text limit of the target (0: none) .IsCollapsedthe cut body belongs in a collapsed block ( on_long = "blockquote").Stringsthe notices of [strings]:.Strings.NoSubject,.Strings.EmptyBody,.Strings.Truncated,.Strings.MoreAttachments,.Strings.NotSent -
The functions, besides those of
text/template(printf,len,index,urlqueryand the others):Function Result toUpper,toLower,trimSpace,trimEnd,titlethe string in capitals, in small letters, without white space around it, without it at the end, with the first letter of every word capitalized and the rest kept default D VV, orDwhenVis empty or white space onlyjoin SEP LISTthe strings or addresses of LISTjoined bySEP:{{ .To | join ", " }}match RE S,reReplaceAll RE REPL Swhether the Go regular expression REmatches inS;Swith every match replaced,$1inREPLfor a grouptz ZONE T,date LAYOUT TTin the IANA zoneZONE, read from the zone database of the system, without which any zone butUTCandLocalfails the template, as an unknown one does;Tin a Go layout:{{ .Date | tz "UTC" | date "2006-01-02 15:04" }}lines S,head N S,tail N Sthe lines of S, forrange; its first and lastNlinestruncate N SScut toNcharacters, the last three of them...; without them whenNis 3 or lessindent N SNspaces before every line ofShumanizeBytes Na size: 1.5 KiBtoJson VVas a JSON value, always validpathSegment SSescaped for one segment of a URL path,/includedtgHTML,discordEscape,discordCode,slackEscape,slackCode,mmEscape,mmCodethe escapers above -
The text is cut to the limit of the target as a built-in one is (see "Configuration" and "Long messages"): the subject, the attachments and the body are cut and the template rendered again. That works for a template whose text grows with them; what it adds on its own is cut hard, and an
httpbody that does not fitmax_textat all fails the template. -
Cutting a long text renders the template again and again, up to a few dozen times. All the renderings of the text of one message for one target may take 2 s together, ending no later than
deadline, and each one 1 s and 1 MiB of output. Past 1 MiB a text with a limit counts as too long and its body is cut; without a limit (httpwithoutmax_text,shoutrrr) the template fails. A template stopped while rendering, by either time limit, stays failed for the rest of the call or queue run, so that each further message gets the built-in template at once. A rendering due after the time is spent is not started: that message gets the built-in template, and the template stays in use. -
A template that fails while rendering a message (an error of a function, the time or size limit, a text of white space only, an
httpbody overmax_text) leaves that message the built-in template of the target, and the log recordstemplate failed, built-in used(warning) withtargetanderr; the exit status follows the delivery, 0 when the target took it. A Telegram target that answers 400 to the text of the template gets the text of the built-in template once, with the same record, before the full text goes as a file alone (see "Long messages"). -
The path, query and header templates of an
httptarget (see "Targets") are rendered under the same limits, from the same budget as the text; each may render nothing, which leaves the value empty. They have no built-in template to fall back to: when one fails, the target fails permanently, gets nothing for that message, and the log recordsrequest template failed, message not sent(warning) withtargetanderr. A parse error of one of them names<target>.path,<target>.query.<name>or<target>.headers.<name>and its line.
Routes and suppression
[target.ops-telegram]
type = "telegram"
token_file = "/etc/mailcrier.d/tg.token"
chat_id = -1001234567890
[target.mm]
type = "http"
url_file = "/etc/mailcrier.d/mm.url"
preset = "mattermost"
[[suppress]]
subject = "*Anacron*"
[[route]]
subject = "*raid*"
targets = ["ops-telegram"]
continue = true
[[route]]
recipient_regex = '^backup(@|$)'
targets = ["mm"]
[[route]] # no condition: every message
targets = ["mm"]
- Without a
[[route]]every target gets every message, except one whose recipients are all direct chats (<chat>@telegramwithtelegram_direct), which goes to those chats alone. With routes, the rules are checked from the first, for each recipient of the envelope on its own: the targets of the first rule whose conditions all match are taken, and withcontinue = truethe check goes on and the targets of the next matching rules are added. The targets of all recipients are joined: a message torootandbackup@example.orgreaches the targets of both, even when the rule forrootcomes first withoutcontinue. A rule without conditions matches every message. - A condition is
subject,senderorrecipient:subjectis the subject with encoded words decoded and white space collapsed, empty when the message has none;senderis the envelope sender, see "sendmail command line":-for-r, elseResent-From, elseFrom, else the caller; empty for the null sender;recipientis one envelope recipient,Bccincluded, as its address without the display name:Root <root@example.org>on the command line isroot@example.org. A local name stays without a domain: cron mailsroot, whichrecipient = "root"matches androot@*does not.
- Each condition is a glob, or an RE2 expression in Go syntax under the
key with
_regex(subject_regex,sender_regex,recipient_regex); a rule takes at most one of the two keys of a field. A glob matches the whole value with case ignored, Cyrillic and other scripts included:*is any run of characters,/and line breaks included,?one character, and\makes the next character literal; there are no[...]classes. An expression matches anywhere in the value unless anchored with^and$, with case as written unless it starts with(?i). An empty glob or expression is an error: a field without a condition leaves its key out. targetsis required, a list of configured target names without repeats.- A message without recipients, as
-twithout recipient headers gives, is checked once: only a rule withoutrecipientcan match it. - A recipient that no rule matches, while others have targets, is logged
as
no route for recipient(warning) withunrouted, the number of such recipients, never the addresses. When no rule matches at all and no recipient is a direct chat, the message is held inhold/with the reasonno route, the log recordsno route for message(warning), and the call exits 64; with the spool off, or the entry not written, the message is lost (message lost, spool offormessage lost, not held) with the same status. A queue run checks a held message against the rules of the moment and queues it once they select a target; until then it stays, counted in the limits of the spool, and moves tofailed/afterhold_ttl. Until then every later call of the same user and every-qread and route it again, before the retries ofqueue/and out of the samedrain_budgetorrun_budget: held messages up to the per-user limit of the spool take that time from the retries. The warning comes once: a run that finds no route again logs it as debug. The targets chosen are logged asmessage routed(debug) withtargets. - The targets of a queued message are fixed when it is queued: a change
of the routes affects new messages and those released from
hold/, not the retries. - A
[[suppress]]rule takessubjectandsenderas above, or their_regexkeys, at least one of them: a rule without conditions would drop every message. The rules are checked before the routes and before the spool; a message that matches every condition of one of them is neither sent nor spooled, the log recordsmessage suppressed(info) withrule, the number of the rule from 1, and the call exits 0. A message released fromhold/is checked again, and a suppressed one is deleted. - An error in a rule exits 78 like any configuration error and names the
rule by its number and line (
route 3: ...), never its expression. The line is that of the[[route]]or[[suppress]]table; rules written as an inline array,route = [{ ... }, { ... }], all point at the line of its key, and only the number tells them apart.
Direct chats let a caller name a Telegram chat as a recipient:
[general]
telegram_direct = "ops-telegram"
telegram_direct_chats = [1234, -1001234567890, "@ops_channel"]
telegram_direct_max = 10 # optional, the default
[target.ops-telegram]
type = "telegram"
token_file = "/etc/mailcrier.d/tg.token"
chat_id = -1001234567890
- With
telegram_directnaming atelegramtarget, a recipient<chat>@telegram, the domain in any case, gets a copy of that target sent to the chat:1234@telegrama user or bot chat,-1001234567890@telegrama group,@ops_channel@telegrama public chat by its username. The copy has the token, template, limits,on_long,long_fileanddisable_notificationof the target, but nomessage_thread_id: a topic belongs to the chat of the target, and another chat refuses it. Spellings of one chat are one chat: leading zeros of an id and the case of a username do not count. - Only the chats of
telegram_direct_chatsget a copy. Any local user can name a recipient, and with the setgid install users cannot read the token, so this list is what decides where the bot writes for them. It is required withtelegram_direct, holds integers and@usernamestrings (5 to 32 letters, digits or_) without repeats; a string of digits is an error, as are the list andtelegram_direct_maxwithouttelegram_direct(exit 78). A chat not in the list,@telegram, or any other local part is an ordinary recipient, routed like the others and logged asdirect chat not allowedordirect address invalid(warning) withcount, never the address. Withouttelegram_directthese addresses are ordinary recipients without a warning. telegram_direct_max, a positive integer, bounds the copies of one message: the chats past it, counted in the order of the recipients after repeats are dropped, get nothing, and the log recordsdirect chats over limit(warning) withdropped. Every copy goes out with the one token of the bot, and a delay a service asks for, as with a 429, holds back only the copy that got it for the rest of a queue run, not the target the copies are made of nor the other copies.- The routes see only the other recipients. A message to direct chats
alone goes to them alone, even past a rule without conditions; when
the routes select the target of
telegram_directand a direct recipient names the chat of that target, the chat gets one message. With a direct chat, recipients without a route only add theno route for recipientwarning. - A copy is named
<chat>@telegramwith the chat normalized (1234@telegram,@ops_channel@telegram) in the log (target), in the spool and inmailq: the chats come from the configuration, unlike the other addresses. A queued copy is retried with the target and the list of the moment, and fails withtarget removed, message dropped for itoncetelegram_director its chat is gone from the file. - On the command line a negative id reads as an option: recipients go
after
--, as insendmail -- -1001234567890@telegram.
Spool and queue
[spool] # optional; the values are the defaults
dir = "/var/spool/mailcrier" # "" turns the spool off
drain_budget = "10s" # queue run after the own message of a call
drain_max_messages = 20
run_budget = "60s" # queue run of -q
queue_ttl = "168h" # then queue/ -> failed/
hold_ttl = "168h" # then hold/ -> failed/
failed_ttl = "720h" # then deleted
max_queue_messages = 1000
max_queue_bytes = 268435456
max_queue_messages_per_uid = 200
max_queue_bytes_per_uid = 67108864
[target.hook]
type = "http"
url_file = "/etc/mailcrier.d/hook.url"
preset = "generic-json"
- Every message is written to the spool before its delivery: the raw
message as
queue/<id>.emland its state per target asqueue/<id>.json, each first written totmp/, synced and moved intoqueue/. The state is rewritten after each target, and the entry removed once no target waits for it. A target that fails temporarily stays pending, and the call exits 0 with the warningmessage queued(ormessage queued for targetwhen another target has the message): the message is accepted, as a classic MTA accepts it into its queue. - A queue run retries a pending target one minute after its first failure,
then after twice the previous delay, at most 24 hours, and never before
the delay a service asked for with
Retry-After; when a service asks for a delay, the rest of the run skips that target in the other entries. An entry queued longer thanqueue_ttl, counted from its release for a message that was held, moves tofailed/with the errormessage failed, and afterfailed_ttlthere it is deleted. Files older than one hour that a killed process left behind, intmp/unless a live process still holds them and sidecars without their message elsewhere, are removed. - A retry renders the stored message with the configuration and templates
of the moment, to the targets fixed when it was queued. Renaming or
removing a target drops its pending messages: they fail for that target
with
target removed, message dropped for it. - A message whose configuration is rejected goes to
hold/(of the default directory when the file cannot be read) and the call exits 78; the first queue run with a valid configuration routes it as a new message, see "Routes and suppression". When the valid file names adirother than the default,-qalso moves whathold/of the default directory keeps into that queue.hold_ttlbounds the wait. - Each call runs the queue for at most
drain_budgetanddrain_max_messagesentries after delivering its own message, only for the entries of its caller's uid, and not at all while another call of the same user is doing so (locks/drain-<uid>.lock).mailcrier -qruns it for at mostrun_budget: root, themailcrieruser and a process without the setgid bit take every entry, any other user only their own. An entry is locked while a process works on it, and a run skips locked entries, so parallel runs never deliver an entry twice. - The packages run
mailcrier -qasmailcrierevery 5 minutes: the systemd timermailcrier-queue.timer(its service stops a run after 3 minutes, which coversrun_budgetof 60 seconds and the delivery under way; a largerrun_budgetneeds a largerTimeoutStartSecin a drop-in), or where systemd is not running/etc/cron.d/mailcrier, or/etc/crontabs/mailcrieron Alpine, all without mail from cron since-qlogs to syslog only (MAILTO=for BusyBox crond, which takesMAILTO=""for an address). Without a running cron daemon or timer, as in most containers, only the calls themselves run the queue; a container that sends rarely runsmailcrier -qfrom a scheduler such as supercronic, a sidecar or a health check. - Limits: all entries in
tmp/,queue/andhold/together stay withinmax_queue_messagesandmax_queue_bytes, those of one uid within the_per_uidlimits; users other than root andmailcrierfill at most 80% of the totals, so root still gets its mail queued. Parallel calls never exceed a limit together, but near it they may all be refused where one would have fit. A message over a limit is not queued (spool entry not written): it is delivered all the same, and a temporary failure then exits 73. - Delivery is at least once: a process killed after a service accepted the message, or a timeout after the service accepted it, repeats that delivery on the next run, and so may a power failure, since the state written after a delivery is not synced to disk before the next step.
- The spool must be on a local file system: the locks (
flock) do not exclude processes over NFS. The directory must exist; the program createstmp,queue,hold,failedandlocksin it when missing, which suits a container. With the setgid install the packages create the directory and all five asroot:mailcrier 2770: made by the program, they would belong to whichever user sent first. Entries are created with mode0660. mailqand-bplist the entries ofqueue/,hold/andfailed/with the state, attempts, next attempt and last error of each target, for root, themailcrieruser and a process without the setgid bit; anyone else sees one line of counts; a spool directory that does not exist lists asqueue is empty.--statusprints one logfmt line for monitoring,queued=1 held=0 failed=0 tmp=0 bytes=1432 oldest_age_seconds=75, and exits 74 when the directory does not exist. Neither creates anything in the spool.
Options, privileges and containers
--config PATHor the environment variableMAILCRIER_CONFIGnames another configuration file;--configwins. sendmail's-Cis always ignored with a warning.--check-configchecks the configuration, see "Checking the configuration": it reads neither stdin nor the spool and sends nothing, writes its findings to stderr and exits 0 without an error, 78 with one. It takes no arguments (64). root, themailcrieruser and a caller without the setgid bit may run it.--probesends a sample message, subjectmailcrier probe from <host>, to every target of the configuration, or with--probe -- name ...to the targets named (an unknown name exits 64 before anything is sent; the names go after--, since a name may start with-). These are real notifications, andexechooks run. Routes,[[suppress]]rules and direct chats do not apply. It reads no stdin, does not touch the spool and does not run the queue. One logfmt line per target goes to stdout, such astarget=mm class=temp status=503 err="..."; a template that failed and was replaced by the built-in one showstemplate=fallbackwith status 0, and--check-configshows the template error. The exit status is 0 when every target took the message, 69 when one rejected it, and 75 when one failed temporarily and none rejected it. root, themailcrieruser and a caller without the setgid bit may run it. Mail flags such as-for-tare ignored, and of several mode options the last one wins.- The intended install is setgid: binary
root:mailcrier 2755, configuration and*_filefilesroot:mailcrier 0640, so that cron jobs of any user can send while only root and the binary read the secrets. When a user other than root runs the binary and the kernel applies the setgid bit:- before doing anything else the process executes itself once more,
through
/proc/self/exeor else/usr/sbin/mailcrier, with the environment reduced toUSER,LOGNAME,HOME,LANG,LC_*andTZ(a zone name such asEurope/Berlinonly), so thatGODEBUG, proxy or TLS variables of the caller cannot act with the group privilege. If both fail, it replaces its environment the same way and goes on with HTTP/2 off, the Go debug output dropped and no proxy, and logsreexec failedas an error; --configandMAILCRIER_CONFIGare ignored with a warning; with--check-configor--probethey exit 64 instead, so that a mode never works on another file than the one named;--probeand--check-configexit 77 for everyone but root and themailcrieruser.
- before doing anything else the process executes itself once more,
through
- Without the setgid bit taking effect the binary runs with the ids of its
caller, and that is the normal mode for containers: Docker with
--security-opt no-new-privileges, Kubernetes withallowPrivilegeEscalation: false, a systemd unit withNoNewPrivileges=yesorRestrictSUIDSGID=yes, or a binary copied without the bit. The configuration and its*_filefiles must then be readable by the caller (for PHP-FPM, for example0640 root:www-data),--configandMAILCRIER_CONFIGwork, and secrets mounted as files (Docker or Kubernetes secrets) go in through*_file. For PHP setsendmail_path = /usr/sbin/sendmail -t -iwith/usr/sbin/sendmaila link to the binary, and pass the variable withenv[MAILCRIER_CONFIG] = /run/secrets/mailcrier.confin the pool configuration. The spool directory,/var/spool/mailcrierunless[spool] dirnames another, must exist and be writable by the caller (a volume, when queued messages must survive the container); without it a temporary failure exits 73, anddir = ""turns the spool off. - On a host with the setgid install, a caller running under
NoNewPrivileges=yesorRestrictSUIDSGID=yesdoes not get the group: the configuration is unreadable and the call exits 78.
Checking the configuration
mailcrier --check-config loads the configuration and builds its targets
as every call does, so it finds every error that would reject a message
with 78, and adds warnings about what loads but may not work as meant.
The errors and warnings go to stderr, one per line, and a last line counts
them; stdout stays empty. A call stops at the first error: after an error
in the file nothing else is reported, after an error of a target (a
template that does not parse, an unknown shoutrrr service) the warnings
about the file still are. The exit status is 78 with an error and 0
otherwise, warnings included. Run it as root after each change of the
configuration: root gets the group of a setgid binary like any other
user, so the checks of permissions below run.
[target.ops]
type = "http"
url = "https://discord.com/api/webhooks/123/abc/slack"
preset = "slack-webhook"
[target.backup]
type = "discord"
url = "https://discord.com/api/webhooks/456/def"
[[route]]
subject = "*backup*"
targets = ["ops"]
warning: /etc/mailcrier.conf:4:1: target "ops": preset "slack-webhook" on a Discord host does not disable mentions, use type "discord"
warning: /etc/mailcrier.conf:6:9: target "backup": no route names this target
warning: /etc/mailcrier.conf:10:3: routes have no rule without conditions: a message no rule matches is held and the call exits 64, or the message is lost with the spool off
/etc/mailcrier.conf: 0 errors, 3 warnings
| Warning | Cause | What to do |
|---|---|---|
file of key "token_file" is readable by all users (or url_file) |
the secret file has the read bit for others | chmod o-r, owner group mailcrier, mode 0640 |
file is readable by all users and holds secrets |
the configuration writes out token, url, headers, query or path and has the read bit for others |
chmod 0640, or move the secrets into *_file files |
file of key "..." is not readable by the group of the binary, every call of another user exits 78 |
a *_file or template_file, or the configuration itself, belongs to the group of the binary without the read bit for the group, or to another group without the read bit for others: the kernel applies the bits of one class only |
chgrp mailcrier and chmod g+r |
directory of key "..." is not searchable by the group of the binary, ... |
a directory on the way to the file, / included, belongs to the group of the binary without the search bit for the group, or to another group without the search bit for others |
chmod o+x, or group mailcrier with g+x |
shoutrrr service "telegram" has a native target type "telegram" (also slack, discord, ntfy) |
the shoutrrr target sends plain text without the escaping, length limits and files of the service | use the target type of the service |
preset "slack-webhook" on a Discord host does not disable mentions |
the Slack-compatible endpoint of a Discord webhook lets @everyone from a message ping the channel |
use type discord |
routes have no rule without conditions |
routes exist, but no rule matches every message | add a last [[route]] with targets only; a condition such as subject = "*" does not count |
no route names this target |
routes exist, and neither a route nor telegram_direct names the target |
add it to a route, or remove it |
first element of key "argv" is not an executable file |
the program of an exec target is missing or has no execute bit |
install it, chmod +x |
template fails on the sample message, the built-in one is used |
the template from the configuration fails or does not fit the limit of the target | fix the template; calls use the built-in one meanwhile |
request template fails on the sample message, the target gets nothing |
a path, query or headers template fails |
fix the template; the target gets no message meanwhile |
the sample message does not render, the target gets nothing |
the built-in template does not fit max_text, for JSON or MarkdownV2 |
raise max_text |
The four checks of permissions run only when the setgid bit applied to
the process, which is the case for root and for the mailcrier user with
another primary group; without the bit (a container, a binary without
the bit, sudo -u mailcrier with the primary group mailcrier) they are
skipped, because the group that reads the files is unknown. They read the
mode and the owner group only: access control lists are not seen, and for
a directory that is a symbolic link the target directory is checked but
not the directories on the way to it. The check of argv runs with the
ids of the caller of --check-config, while a hook runs with those of
the caller of each message.
The warnings that end in "sample message" come from rendering a built-in
sample message, the one --probe sends, for every target, with nothing
sent and no hook run. The sample has no attachments, no HTML, no long text
and no Bcc, so a template branch for those is not tried; and the URL an
http target builds from its rendered path is checked only when it
sends.
sendmail command line
The binary is meant to be installed as /usr/sbin/sendmail and understands
the command lines of cron, anacron, at, sudo, mdadm, mailx, apt-listchanges,
unattended-upgrades, fail2ban and similar callers. Flags follow getopt: a
value is glued (-froot) or the next argument (-f root), flags without a
value group (-ti), -- ends the options, and options and recipients may
come in any order before it. The value of a flag is never read as an
option: -f --probe names a sender.
| Flags | Value | Effect |
|---|---|---|
-f, -r |
required | envelope sender; '' or <> is the null sender |
-F |
required | sender's full name |
-t |
none | the To, Cc and Bcc headers add recipients, or only the Resent-To, Resent-Cc and Resent-Bcc headers when there are any |
-i, -oi |
none | a line with a single dot is ordinary text |
-o |
required | -oi as above, any other -o option ignored |
-b |
required | by the first letter: m delivers (the default), i does nothing, p lists the queue, s is refused with 64, other letters are ignored with a warning |
-q |
optional, glued only | runs the queue once; an interval (-q30m) is ignored |
-I |
none | does nothing, like -bi |
-C |
required | ignored with a warning; --config names another configuration |
-B, -h, -L, -N, -O, -R, -V, -X, -p, -A |
required | ignored |
-d, -e |
glued, or the next argument unless it starts with - |
ignored |
-v, -m, -n, -U, -G |
none | ignored |
| any other letter | none | ignored with an unknown option warning naming the flag |
- Long options are
--config PATH(or--config=PATH),--version,--help,--probe,--check-configand--status; an unknown one is logged by name, without its value, and ignored. A group of unknown letters yields one warning, and a command line at most 16 plus onewarnings suppressedrecord with the count. A flag sendmail knows but mailcrier does not never stops the call: refusing it would lose the message of a cron job. - Called as
newaliasesthe binary does nothing and exits 0; called asmailqit lists the queue, see "Spool and queue"./etc/aliasesand.forwardare not read, and-bvand-btare ignored like other unsupported-bmodes; choosing targets by recipient is left to routes in the configuration. - Recipients are the arguments, split at commas
(
a@example.org, b@example.orgin one argument, ora@example.org,andb@example.orgas Debian cron passes them), and with-tthe addresses of theTo,CcandBccheaders; duplicates are dropped. A message with aResent-To,Resent-CcorResent-Bccheader, even an empty one, is a resent message:-ttakes the addresses of these three headers instead ofTo,CcandBcc, as Postfix does.BccandResent-Bccheaders are removed from the message and never reach a notification. A message without any recipient is delivered to every target when there are no routes, see "Routes and suppression". - The sender is the value of
-for-rwithout surrounding angle brackets, else the firstResent-Fromaddress, else theFromaddress, elseEMAIL, elseUSERorLOGNAMEat the host name, else the login name of the caller's uid at the host name. A setgid-elevated process skips the environment and uses the login name of the uid. When the uid has no entry in the user database, as in a container started with a numeric user, the sender is empty. - A line break in
-f,-r,-For a recipient argument exits 64. An address of aFrom,To,CcorBccheader or of theirResent-counterparts that holds a control character is dropped with a warning. - The message: CRLF line ends become LF; a leading mbox envelope line
(
Fromand a space) is dropped; without-ia line with a single dot ends the message and the first dot of a line starting with two dots is removed, as in sendmail 8 (fail2ban passes no-i); input whose first line is no header field, a continuation line included, is all body; a later line with a space before the colon or without a colon starts the body and logs a warning. Input beyond 10 MiB is read and discarded with a warning. A body without visible text is sent as(empty body), or theempty_bodytext of[strings]. - MIME:
Content-TypeandContent-Transfer-Encodingare honoured with or withoutMIME-Version. The text is thetext/plainparts, named or not, unlessContent-Disposition: attachmentmarks them, else the firsttext/htmlpart, also one insidemultipart/related, converted to text (scripts, styles and embedded objects dropped, link targets in parentheses after the link text, links other thanhttp,https,mailtoandftpreduced to their text, white space collapsed outsidepre, and several empty lines in a row reduced to one everywhere,preincluded); every other part is an attachment. Multipart nesting deeper than 8 levels is kept as one attachment; a broken multipart structure keeps the parts before the damage and logs a warning. - Charsets: encoded words (RFC 2047), quoted-printable and base64 are
decoded, and text is converted to UTF-8. A body declared
US-ASCII(cron under the C locale saysANSI_X3.4-1968) or in an unknown charset is taken as UTF-8 when it is valid UTF-8. Text without a charset or declaredUS-ASCIIthat is not valid UTF-8, and text declaredISO-8859-1, is read as windows-1252, as browsers do: bytes 0x80-0x9F are the quotes, dashes and euro sign of that charset. A charset that browsers refuse to decode, such as ISO-2022-KR, counts as unknown. Encoded words follow the same rules. Bytes that stay invalid become U+FFFD. The subject has its white space collapsed. - The log records the size of the message and the number of recipients, never the addresses; only the copy for a direct chat carries the chat in its name, see "Routes and suppression".
Exit status
| Situation | Status |
|---|---|
| at least one target accepted the message; the targets that failed temporarily are queued | 0 |
a [[suppress]] rule matched; the message is neither sent nor spooled |
0 |
| every target failed temporarily and the message is queued | 0 |
newaliases, -bi, -I, mailq, -bp, -q, --status, --version, --help |
0 |
--check-config found no error, with or without warnings |
0 |
--probe: every target took the sample message, if only with template=fallback |
0 |
| no target accepted it and one rejected it, or all failed temporarily with the spool off | 69 |
--probe: a target rejected the sample message |
69 |
| a target failed temporarily and the spool entry could not be created: directory missing or not writable, or a limit reached | 73 |
a target failed temporarily and the spool entry could not be written; -q or --status could not read the spool, mailq a spool that exists |
74 |
--probe only: a target failed temporarily and none rejected the sample message; nothing is queued |
75 |
usage error: -f or -r without a value, a line break in the sender, the full name or a recipient, -bs, --config without a value; stdin is not read |
64 |
--check-config with an argument, --probe naming no configured target, or either from an elevated caller with --config or MAILCRIER_CONFIG |
64 |
| no route selects a target; the message is held, or lost with the spool off | 64 |
| stdin cannot be read | 66 |
| panic in the main goroutine (a bug; the redacted record in the log carries the details) | 70 |
--probe or --check-config from an elevated caller other than root and the mailcrier user |
77 |
| for a call with a message: the configuration cannot be read, parsed or validated, defines no targets, or holds a template that does not parse; the message is held | 78 |
--check-config found an error, or --probe cannot use the configuration; nothing is held |
78 |
The targets are sent to at the same time. A panic while rendering or
sending for one target (a bug) fails that target permanently, logged
redacted as target failed, and the others still deliver. A panic in any
other goroutine ends the process with the Go runtime's own report on
stderr and status 2; that report is not redacted. A target that failed is
logged as target failed; a temporary failure is also logged as
message queued for target or message queued, or, without a spool
entry, as message lost for target when another target accepted the
message and as message lost when every target failed temporarily.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
mailcrier
command
Command mailcrier is a sendmail replacement that delivers the mail of cron, at and other system tools to chat and webhook services.
|
Command mailcrier is a sendmail replacement that delivers the mail of cron, at and other system tools to chat and webhook services. |
|
internal
|
|
|
app
Package app wires configuration, message, targets and delivery into one sendmail invocation.
|
Package app wires configuration, message, targets and delivery into one sendmail invocation. |
|
backend
Package backend holds the types shared by every delivery target: the Sender interface, target capabilities, the rendered payload and the classified delivery error.
|
Package backend holds the types shared by every delivery target: the Sender interface, target capabilities, the rendered payload and the classified delivery error. |
|
backend/discord
Package discord implements the discord target type: one execution of a channel webhook per message, with the attachments as files.
|
Package discord implements the discord target type: one execution of a channel webhook per message, with the attachments as files. |
|
backend/hook
Package hook implements the exec target type: one run of a program per message, with the message on its stdin and the envelope in its environment.
|
Package hook implements the exec target type: one run of a program per message, with the message on its stdin and the envelope in its environment. |
|
backend/ntfy
Package ntfy implements the ntfy target type: a JSON publish for the text and one more message per attachment, which ntfy allows one of.
|
Package ntfy implements the ntfy target type: a JSON publish for the text and one more message per attachment, which ntfy allows one of. |
|
backend/shoutrrr
Package shoutrrr implements the shoutrrr target type: one send per message through a service of github.com/nicholas-fedor/shoutrrr, chosen by the scheme of the URL.
|
Package shoutrrr implements the shoutrrr target type: one send per message through a service of github.com/nicholas-fedor/shoutrrr, chosen by the scheme of the URL. |
|
backend/slack
Package slack implements the slack target type through the Slack Web API: chat.postMessage for the text and the external upload methods for the attachments, which land in the thread of the message.
|
Package slack implements the slack target type through the Slack Web API: chat.postMessage for the text and the external upload methods for the attachments, which land in the thread of the message. |
|
backend/telegram
Package telegram implements the telegram target type: the Bot API methods sendMessage for the text and sendDocument for each attachment, the first one with the text as caption when it is short enough.
|
Package telegram implements the telegram target type: the Bot API methods sendMessage for the text and sendDocument for each attachment, the first one with the text as caption when it is short enough. |
|
backend/webhook
Package webhook implements the http target type: one request per message, POST unless configured otherwise, with the JSON document its template renders, or without a body for GET.
|
Package webhook implements the http target type: one request per message, POST unless configured otherwise, with the JSON document its template renders, or without a body for GET. |
|
config
Package config loads and validates the mailcrier TOML configuration.
|
Package config loads and validates the mailcrier TOML configuration. |
|
delivery
Package delivery renders a message for every target, sends it to all of them at once and turns the per-target outcomes into the process exit status.
|
Package delivery renders a message for every target, sends it to all of them at once and turns the per-target outcomes into the process exit status. |
|
golden
Package golden compares test output with golden files in txtar format: one file per fixture, one section per output.
|
Package golden compares test output with golden files in txtar format: one file per fixture, one section per output. |
|
message
Package message reads the mail a caller pipes into sendmail and exposes the parts a notification needs: decoded headers, the text of the body in UTF-8 and the attachments.
|
Package message reads the mail a caller pipes into sendmail and exposes the parts a notification needs: decoded headers, the text of the body in UTF-8 and the attachments. |
|
redact
Package redact replaces registered secrets with a mask in everything the process logs.
|
Package redact replaces registered secrets with a mask in everything the process logs. |
|
render
Package render turns a message into the text of one target with Go templates and fits that text into the target's length limit.
|
Package render turns a message into the text of one target with Go templates and fits that text into the target's length limit. |
|
route
Package route selects the targets of a message and decides which messages are suppressed.
|
Package route selects the targets of a message and decides which messages are suppressed. |
|
sendmail
Package sendmail parses a sendmail command line: flags in getopt style, the mode selected by argv[0], and the recipients.
|
Package sendmail parses a sendmail command line: flags in getopt style, the mode selected by argv[0], and the recipients. |
|
spool
Package spool keeps messages on disk until every target has them.
|
Package spool keeps messages on disk until every target has them. |
|
text
Package text escapes, measures and truncates text for the markup of each target.
|
Package text escapes, measures and truncates text for the markup of each target. |
|
scripts
|
|
|
check-commits
command
Command check-commits fails when a commit in REV..HEAD does not follow Conventional Commits: the changelog is generated from commit subjects, and a malformed subject drops the change from it.
|
Command check-commits fails when a commit in REV..HEAD does not follow Conventional Commits: the changelog is generated from commit subjects, and a malformed subject drops the change from it. |
|
check-refs
command
Command check-refs fails when commit messages, code comments or documentation changed since a base revision refer to plan stages or to review and decision IDs.
|
Command check-refs fails when commit messages, code comments or documentation changed since a base revision refer to plan stages or to review and decision IDs. |
|
check-release
command
Command check-release fails unless the CI of a commit passed: a release is built from a tag, and the tag must point at a commit whose push run of the CI workflow on the development branch finished with every required job successful.
|
Command check-release fails unless the CI of a commit passed: a release is built from a tag, and the tag must point at a commit whose push run of the CI workflow on the development branch finished with every required job successful. |