Shell Script Client to Receive Email API.

#!/bin/sh
#
# Shell client script for Receive Email API (https://1secmail.net)
#
# Dependencies: curl, jq
# Author: [Mitch Weaver](https://github.com/mitchweaver)
# Updated by: Jon LaBelle
#
# Source: https://github.com/mitchweaver/bin/blob/master/OLD/1secmail
# Also see: https://github.com/sdushantha/tmpmail
#

usage() {
    cat >&2 << EOF

Usage: ${0##*/} [options] <email>

    -g               generate email address
    -c <email>       check email
    -v <id> <email>  view email at id
    -f <email>       view first email in inbox

EOF
    exit 1
}

die() {
    print >&2 '%s\n' "$*"
    exit 1
}

check_deps() {
    for dep in curl jq; do
        command -v $dep > /dev/null \
            || die "Missing dependency: $dep"
    done
}

gen_email() {
    # Note: "LC_ALL=C" fixes "tr: Illegal byte sequence" error on macOS. Environment LC_ALL was "en_US.UTF-8".
    acc=$(head /dev/urandom | LC_ALL=C tr -dc a-z0-9 | cut -c1-10)
    read -r tld << EEOF
$(
        sort -R << EOF
com
net
org
EOF
    )
EEOF
    printf '%s@1secmail.%s\n' "$acc" "$tld"
}

validate_email() {
    case $1 in
        *@????????.???) ;;
        *) usage ;;
    esac
}

check_mail() {
    validate_email "$1"
    acc=${1%@*}
    tld=${1#*.}
    curl -sL "https://1secmail.$tld/api/v1/?action=getMessages&login=$acc&domain=1secmail.$tld" | jq -r
}

view_mail() {
    validate_email "$2"
    case $1 in
        [0-9][0-9][0-9][0-9][0-9][0-9]*) ;;
        *) usage ;;
    esac
    acc=${2%@*}
    tld=${2#*.}
    curl -sL "https://www.1secmail.$tld/api/v1/?action=readMessage&login=$acc&domain=1secmail.$tld&id=$1" \
        | jq -r '.body'
}

view_first() {
    validate_email "$1"
    id=$(check_mail "$1" | grep id | head -n 1 | grep -oE '[0-9]+')
    view_mail "$id" "$1"
}

main() {
    check_deps
    case ${1#-} in
        g) gen_email ;;
        c) check_mail "$2" ;;
        v) view_mail "$2" "$3" ;;
        1 | f) view_first "$2" ;;
        *) usage ;;
    esac
}

main "$@"