Skip to content

Instantly share code, notes, and snippets.

@raphaelyancey
Last active February 4, 2020 15:39
Show Gist options
  • Save raphaelyancey/0bd02befe5f4f2b1758c3a89635f1bb9 to your computer and use it in GitHub Desktop.
Save raphaelyancey/0bd02befe5f4f2b1758c3a89635f1bb9 to your computer and use it in GitHub Desktop.
One-line SMTP send with curl
#!/bin/bash
# Sends an email with the params:
# - CURL_SMTP_SERVER: SMTP server URL in the form of 'smtp(s)://host:port'
# - CURL_SMTP_FROM: the sender e-mail address
# - CURL_SMTP_TO: the recipient e-mail address
# - CURL_SMTP_USER: the SMTP user (optional)
# - CURL_SMTP_PASSWD: the SMTP user's password (optional — ignored if CURL_SMTP_USER is empty)
#
# Usage:
# ./ezmail.sh subject [body]
# (if no body, reads from stdin)
curl -sS \
--url $CURL_SMTP_SERVER \
--mail-from $CURL_SMTP_FROM \
--mail-rcpt $CURL_SMTP_TO \
$(test -n ${CURL_SMTP_USER-''} && echo -ne '--user '$CURL_SMTP_USER':' && test -n ${CURL_SMTP_PASSWD-''} && echo -ne $CURL_SMTP_PASSWD) \
-T <(echo -e 'From: '$CURL_SMTP_FROM'\nTo: '$CURL_SMTP_TO'\nSubject: '$1'\n\n'${2:-/dev/stdin})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment