================================================================================
SENDER.SH - NOTIFICATION SCRIPT README
================================================================================

Overview
--------
`sender.sh` is a lightweight Bash utility designed to send email and SMS
notifications using Gmail SMTP over SSL/TLS (`smtps://smtp.gmail.com:465`) via
`curl`. It supports logging all transmission attempts (successes and failures)
and is optimized for standalone manual use, chained pipelines, and automated
cron jobs.

--------------------------------------------------------------------------------
1. Prerequisites & Setup
--------------------------------------------------------------------------------
1. Make sure `curl` is installed:
   which curl || sudo apt-get install curl

2. Grant executable permissions to the script:
   chmod +x /path/to/sender.sh

3. Open `sender.sh` and ensure the configuration section has your settings:
   - `sender`: Your Gmail address.
   - `receiver`: Destination email address.
   - `receiver2`: Second destination (e.g., SMS via gateway `number@msg.fi.google.com`).
   - `gapp`: Your 16-character Google App Password (not your normal password).
   - `sub`: Notification subject line.
   - `logfile`: Absolute path where logs should be appended (e.g., `/var/log/sender.log`
     or `/home/user/logs/sender.log`). Make sure the running user has write permissions!

--------------------------------------------------------------------------------
2. Command Syntax & Usage
--------------------------------------------------------------------------------

A. Manual Command Line Execution
--------------------------------
Pass the message body directly as a quoted argument:

  ./sender.sh "There was a big time problem with the sync."

If calling from anywhere using an absolute path:

  /usr/local/bin/sender.sh "There was a big time problem with the sync."

Passing Multi-line Messages or Output from stdin:
The script automatically falls back to standard input if no argument is passed:

  echo "Sync failed at $(date)" | /usr/local/bin/sender.sh
  cat /var/log/app_failure.log | /usr/local/bin/sender.sh

Conditional Execution in Shell Scripts / One-Liners:
Trigger a notification only when a command fails:

  /opt/scripts/backup.sh || /usr/local/bin/sender.sh "Backup routine failed on server!"


B. Automated Execution via Cron
-------------------------------
When running from cron, always specify full absolute paths for the script,
environment tools, and files.

Open your user crontab editor:
  crontab -e

Examples:

1. Alert on scheduled maintenance check failure:
   0 3 * * * /opt/scripts/sync_data.sh || /usr/local/bin/sender.sh "There was a big time problem with the sync."

2. Daily sync with immediate alert if exit code is non-zero:
   30 2 * * * /usr/bin/rsync -avz /data/ /backup/ || /usr/local/bin/sender.sh "RSync backup job failed."

3. Send output of a command directly:
   0 8 * * 1 /usr/local/bin/check_disk.sh | /usr/local/bin/sender.sh

Note on Cron Environments:
Cron runs in a minimal environment. Ensure that:
- The script has a `#!/bin/bash` shebang.
- The `logfile` directory configured inside `sender.sh` is writable by the cron user.

--------------------------------------------------------------------------------
3. Execution Lifecycle (What Happens Behind the Scenes)
--------------------------------------------------------------------------------

When you run:
  sender.sh "There was a big time problem with the sync."

1. Initialization & Argument Parsing:
   - The script loads configuration variables (`sender`, `receiver`, `gapp`, etc.).
   - It captures parameter `$1` ("There was a big time problem with the sync.") into
     the internal `$body` variable.
   - If `$1` is missing, it reads from standard input (`/dev/stdin`). If both are
     empty, a default "No content provided." message is supplied.

2. MIME/RFC Header Construction:
   - A secure, unique temporary file is generated via `mktemp /tmp/msg.XXXXXX`.
   - A signal trap (`trap 'rm -f "$tmpfile"' EXIT INT TERM`) is registered to
     guarantee the temporary file is deleted when the script terminates, even if
     interrupted.
   - The script generates a standard RFC 2822/5322 email payload:
     * `From`: Configured sender name and email.
     * `To`: Primary and secondary recipient addresses.
     * `Subject`: Configured subject header.
     * `Date`: Generated system timestamp in standard mail format.
     * Blank line (`\r\n` / `\n\n`): Required separator between headers and payload.
     * Message body: Your text argument.

3. Secure SMTP Transmission:
   - The script invokes `curl` targeting `smtps://smtp.gmail.com:465`.
   - `--ssl-reqd` enforces end-to-end TLS encryption.
   - Credentials (`$sender:$gapp`) authenticate against Gmail's SASL PLAIN/LOGIN.
   - Recipient envelopes (`--mail-rcpt`) are set for both the primary email and
     the SMS gateway address.
   - The formatted temporary message file is uploaded to the SMTP server.
   - Standard output and error streams are captured, preventing terminal spam while
     preserving error diagnostic info.

4. Logging & Clean Exit:
   - The exit code of `curl` is checked (`$?`):
     * If 0: An entry tagged `[SUCCESS]` is written to `"$logfile"`.
     * If non-zero: An entry tagged `[ERROR]` is written to `"$logfile"` containing
       the return code and exact error diagnostics returned by `curl`.
   - The temporary file in `/tmp/` is purged automatically.
   - The script terminates with the appropriate exit status.
================================================================================
