aboutsummaryrefslogtreecommitdiff
path: root/_tils/2020-09-04-send-emails-using-the-command-line-for-fun-and-profit.md
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2020-09-04 08:25:34 -0300
committerEuAndreh <eu@euandre.org>2020-09-04 08:27:08 -0300
commit9748540a28c2257ecc96b8c3a2adc423c137537e (patch)
tree7c93fbc0dbb568f068b8c519e907698d92d40df2 /_tils/2020-09-04-send-emails-using-the-command-line-for-fun-and-profit.md
parentdb post: better pinpoint sqlite unsuitability (diff)
downloadeuandre.org-9748540a28c2257ecc96b8c3a2adc423c137537e.tar.gz
euandre.org-9748540a28c2257ecc96b8c3a2adc423c137537e.tar.xz
Add til on CLI email
Diffstat (limited to '_tils/2020-09-04-send-emails-using-the-command-line-for-fun-and-profit.md')
-rw-r--r--_tils/2020-09-04-send-emails-using-the-command-line-for-fun-and-profit.md76
1 files changed, 76 insertions, 0 deletions
diff --git a/_tils/2020-09-04-send-emails-using-the-command-line-for-fun-and-profit.md b/_tils/2020-09-04-send-emails-using-the-command-line-for-fun-and-profit.md
new file mode 100644
index 0000000..f930a08
--- /dev/null
+++ b/_tils/2020-09-04-send-emails-using-the-command-line-for-fun-and-profit.md
@@ -0,0 +1,76 @@
+---
+title: Send emails using the command line for fun and profit!
+date: 2020-09-04
+layout: til
+lang: en
+ref: send-emails-using-the-command-line-for-fun-and-profit
+---
+Here are a few reasons why:
+
+1. send yourself and other people notification of cronjobs, scripts runs, CI
+ jobs, etc.
+
+2. leverage the POSIX pipe `|`, and pipe emails away!
+
+3. because you can.
+
+Reason 3 is the fun part, reasons 1 and 2 are the profit part.
+
+First [install and configure SSMTP][ssmtp] for using, say, Gmail as the email
+server:
+
+```shell
+# file /etc/ssmtp/ssmtp.conf
+FromLineOverride=YES
+MailHub=smtp.gmail.com:587
+UseSTARTTLS=YES
+UseTLS=YES
+rewriteDomain=gmail.com
+root=username@gmail.com
+AuthUser=username
+AuthPass=password
+```
+
+Now install [GNU Mailutils][gnu-mailutils] (`sudo apt-get install mailutils` or the
+equivalent on your OS), and send yourself your first email:
+
+```shell
+echo body | mail -aFrom:email@example.com email@example.com -s subject
+```
+
+And that's about it, you've got mail. Here are some more places where it might
+be applicable:
+
+```shell
+# report a backup cronjob, attaching logs
+set -e
+
+finish() {
+ status=$?
+ if [[ $status = 0 ]]; then
+ STATUS="SUCCESS (status $status)"
+ else
+ STATUS="FAILURE (status $status)"
+ fi
+
+ mail user@example.com \
+ -s "Backup job report on $(hostname): ${STATUS}" \
+ --content-type 'text/plain; charset=utf-8' \
+ -A"$LOG_FILE" <<< 'The log report is in the attachment.'
+}
+trap finish EXIT
+
+do-long-backup-cmd-here
+
+# share the output of a cmd with someone
+some-program | mail someone@example.com -s "The weird logs that I was talking about"
+```
+
+...and so on.
+
+You may consider adding a `alias mail='mail -aFrom:email@example.com'` so you
+don't keep re-entering the "From: " part.
+
+[ssmtp]: https://wiki.archlinux.org/index.php/SSMTP
+[gnu-mailutils]: https://mailutils.org/
+[forwarding-wiki-section]: https://wiki.archlinux.org/index.php/SSMTP#Forward_to_a_Gmail_mail_server