merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler-merchant-report-generator-email (3868B)


      1 #!/bin/bash
      2 #
      3 # This file is part of TALER
      4 # Copyright (C) 2025 Taler Systems SA
      5 #
      6 # TALER is free software; you can redistribute it and/or modify it under the
      7 # terms of the GNU General Public License as published by the Free Software
      8 # Foundation; either version 3, or (at your option) any later version.
      9 #
     10 # TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     13 #
     14 # You should have received a copy of the GNU General Public License along with
     15 # TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     16 #
     17 
     18 #
     19 # Script to email Taler merchant reports using the UNIX mail command.
     20 # Reads report data from stdin and sends it via email with appropriate formatting.
     21 #
     22 # Usage: taler-merchant-report-generator-email -d DESCRIPTION -m MIME_TYPE -t TARGET_ADDRESS
     23 #
     24 
     25 set -eu
     26 
     27 DESCRIPTION=""
     28 MIME_TYPE=""
     29 TARGET_ADDRESS=""
     30 TMPDIR="${TMPDIR:-/tmp}"
     31 
     32 while getopts "d:m:t:h" opt; do
     33   case $opt in
     34     d)
     35       DESCRIPTION="$OPTARG"
     36       ;;
     37     m)
     38       MIME_TYPE="$OPTARG"
     39       ;;
     40     t)
     41       TARGET_ADDRESS="$OPTARG"
     42       ;;
     43     h)
     44       echo "Usage: $0 -d DESCRIPTION -m MIME_TYPE -t EMAIL_ADDRESS"
     45       echo ""
     46       echo "Sends reports via email."
     47       echo ""
     48       echo "Options:"
     49       echo "  -d DESCRIPTION      Subject line for the email"
     50       echo "  -m MIME_TYPE        MIME type of the report (e.g., text/plain, application/pdf)"
     51       echo "  -t EMAIL_ADDRESS    Email address to send the report to"
     52       echo "  -h                  Show this help message"
     53       echo ""
     54       echo "The report data is read from stdin."
     55       exit 0
     56       ;;
     57     \?)
     58       echo "Invalid option: -$OPTARG" >&2
     59       echo "Use -h for help" >&2
     60       exit 1
     61       ;;
     62   esac
     63 done
     64 
     65 if [ -z "$DESCRIPTION" ];
     66 then
     67   echo "Error: Description (-d) is required" >&2
     68   exit 1
     69 fi
     70 
     71 if [ -z "$MIME_TYPE" ];
     72 then
     73   echo "Error: MIME type (-m) is required" >&2
     74   exit 1
     75 fi
     76 
     77 if [ -z "$TARGET_ADDRESS" ];
     78 then
     79   echo "Error: Target address (-t) is required" >&2
     80   exit 1
     81 fi
     82 
     83 # Validate email address format (basic check)
     84 if ! echo "$TARGET_ADDRESS" | grep -qE '^[^@]+@[^@]+\.[^@]+$';
     85 then
     86   echo "Error: Invalid email address format: $TARGET_ADDRESS" >&2
     87   exit 1
     88 fi
     89 
     90 if ! command -v mail >/dev/null 2>&1;
     91 then
     92   echo "Error: 'mail' command not found." >&2
     93   exit 1
     94 fi
     95 if ! command -v uuencode >/dev/null 2>&1;
     96 then
     97   echo "Error: 'uuencode' command not found." >&2
     98   exit 1
     99 fi
    100 
    101 # Normalize MIME type to lowercase for comparison
    102 MIME_TYPE=$(echo "$MIME_TYPE" | tr '[:upper:]' '[:lower:]')
    103 
    104 # Handle different MIME types
    105 case "$MIME_TYPE" in
    106   text/plain)
    107     # For plain text, send directly as email body
    108     mail -s "$DESCRIPTION" "$TARGET_ADDRESS"
    109     ;;
    110 
    111   *)
    112     # For all other MIME types, create a MIME attachment
    113     # Create temporary files
    114     TMPFILE=$(mktemp "$TMPDIR/taler-report.XXXXXX")
    115     MIMEFILE=$(mktemp "$TMPDIR/taler-mime.XXXXXX")
    116 
    117     # Ensure cleanup on exit
    118     trap "rm -f '$TMPFILE' '$MIMEFILE'" EXIT
    119 
    120     # Save stdin to temporary file
    121     cat - > "$TMPFILE"
    122 
    123     # Determine file extension based on MIME type
    124     case "$MIME_TYPE" in
    125       application/pdf)
    126         EXT="pdf"
    127         ;;
    128       application/json)
    129         EXT="json"
    130         ;;
    131       text/html)
    132         EXT="html"
    133         ;;
    134       text/csv)
    135         EXT="csv"
    136         ;;
    137       application/xml)
    138         EXT="xml"
    139         ;;
    140       application/zip)
    141         EXT="zip"
    142         ;;
    143       image/png)
    144         EXT="png"
    145         ;;
    146       image/jpeg)
    147         EXT="jpg"
    148         ;;
    149       *)
    150         EXT="dat"
    151         ;;
    152     esac
    153 
    154     FILENAME="report.$EXT"
    155 
    156     # Use uuencode method (works with traditional mail command)
    157     uuencode "$TMPFILE" "$FILENAME" | mail -s "$DESCRIPTION" "$TARGET_ADDRESS"
    158     ;;
    159 esac
    160 
    161 exit 0