taler-merchant-report-generator-email (4094B)
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). The local part must not 84 # start with '-': such an address would be taken for an option by the 85 # mail command below. The "--" there stops option processing as well, 86 # but not every mail implementation honours it. 87 if ! echo "$TARGET_ADDRESS" | grep -qE '^[^-@][^@]*@[^@]+\.[^@]+$'; 88 then 89 echo "Error: Invalid email address format: $TARGET_ADDRESS" >&2 90 exit 1 91 fi 92 93 if ! command -v mail >/dev/null 2>&1; 94 then 95 echo "Error: 'mail' command not found." >&2 96 exit 1 97 fi 98 if ! command -v uuencode >/dev/null 2>&1; 99 then 100 echo "Error: 'uuencode' command not found." >&2 101 exit 1 102 fi 103 104 # Normalize MIME type to lowercase for comparison 105 MIME_TYPE=$(echo "$MIME_TYPE" | tr '[:upper:]' '[:lower:]') 106 107 # Handle different MIME types 108 case "$MIME_TYPE" in 109 text/plain) 110 # For plain text, send directly as email body 111 mail -s "$DESCRIPTION" -- "$TARGET_ADDRESS" 112 ;; 113 114 *) 115 # For all other MIME types, create a MIME attachment 116 # Create temporary files 117 TMPFILE=$(mktemp "$TMPDIR/taler-report.XXXXXX") 118 MIMEFILE=$(mktemp "$TMPDIR/taler-mime.XXXXXX") 119 120 # Ensure cleanup on exit 121 trap "rm -f '$TMPFILE' '$MIMEFILE'" EXIT 122 123 # Save stdin to temporary file 124 cat - > "$TMPFILE" 125 126 # Determine file extension based on MIME type 127 case "$MIME_TYPE" in 128 application/pdf) 129 EXT="pdf" 130 ;; 131 application/json) 132 EXT="json" 133 ;; 134 text/html) 135 EXT="html" 136 ;; 137 text/csv) 138 EXT="csv" 139 ;; 140 application/xml) 141 EXT="xml" 142 ;; 143 application/zip) 144 EXT="zip" 145 ;; 146 image/png) 147 EXT="png" 148 ;; 149 image/jpeg) 150 EXT="jpg" 151 ;; 152 *) 153 EXT="dat" 154 ;; 155 esac 156 157 FILENAME="report.$EXT" 158 159 # Use uuencode method (works with traditional mail command) 160 uuencode "$TMPFILE" "$FILENAME" | mail -s "$DESCRIPTION" -- "$TARGET_ADDRESS" 161 ;; 162 esac 163 164 exit 0