aboutsummaryrefslogtreecommitdiffstats
path: root/executables/bin/msmtp-send
blob: fad2f9ec9596519f846c378bedb759dd15096fd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# Smart sendmail replacement with account selection and offline queuing
#
# When the mail server is offline, emails are queued in a local directory.
# When the server is back online, queued emails are sent automatically.
set -euo pipefail

MSMTP_ACCOUNT=""
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}"
QUEUE_DIR="${DATA_DIR}/msmtp-queue"
TEMP_DIR="${DATA_DIR}/msmtp-temp"
MSMTP_PATH="$(command -v msmtp)"

main() {
  parse_args "$@"

  if ! command -v "$MSMTP_PATH" >/dev/null 2>&1; then
    log_message "ERROR" "msmtp not found at $MSMTP_PATH"
    exit 1
  fi

  setup_directories

  local temp_file
  temp_file=$(create_temp_file)
  if ! cat >"$temp_file"; then
    log_message "ERROR" "Failed to read email content"
    cleanup_temp_file "$temp_file"
    exit 1
  fi

  send_email "$temp_file" "$MSMTP_ACCOUNT"
}

parse_args() {
  while [[ $# -gt 0 ]]; do
    case $1 in
    --account)
      if [[ -z "${2:-}" ]]; then
        log_message "ERROR" "--account requires an argument"
        print_usage
        exit 1
      fi
      MSMTP_ACCOUNT="$2"
      shift 2
      ;;
    -h | --help)
      print_usage
      exit 0
      ;;
    esac
  done

  if [[ -z "$MSMTP_ACCOUNT" ]]; then
    log_message "ERROR" "No account specified. Use --account <account> to specify an account."
    print_usage
    exit 1
  fi
}

log_message() {
  local level="$1"
  shift
  echo "$(date '+%Y-%m-%d %H:%M:%S') [$level] $*" >&2
}

print_usage() {
  cat <<USAGE
Usage: $(basename "$0") --account <account> [OPTIONS] [RECIPIENT...]

A sendmail replacement that routes emails through msmtp with offline queuing.

ARGUMENTS:
    --account <account>  Specify the account to use. It should match the name in your
                         msmtp configuration.
    RECIPIENT...         Email recipients (optional, can be specified in email body)

OPTIONS:
    -h, --help         Show this help message

EXAMPLES:
    echo "test email" | $(basename "$0") --account gmail user@example.com

NOTES:
    - Emails are queued offline and sent when connection is available
    - Queue directory: $QUEUE_DIR
USAGE
}

create_temp_file() {
  local timestamp
  timestamp="$(date +%s)"
  local pid="$$"
  local random
  random="$(od -An -N4 -tx4 </dev/urandom | tr -d ' ')"
  echo "$TEMP_DIR/${timestamp}-${pid}-${random}"
}

setup_directories() {
  if ! mkdir -p "$QUEUE_DIR" "$TEMP_DIR"; then
    log_message "ERROR" "Failed to create directories"
    exit 1
  fi
}

cleanup_temp_file() {
  local temp_file="$1"
  rm -f "$temp_file"
}

send_email() {
  local temp_file="$1"
  local account="$2"

  if "$MSMTP_PATH" --account="$account" -t <"$temp_file" 2>/dev/null; then
    log_message "INFO" "Email sent successfully via $account"
    cleanup_temp_file "$temp_file"
    return 0
  else
    log_message "INFO" "Failed to send email, queuing for later"
    queue_email "$temp_file" "$account"
    return 1
  fi
}

queue_email() {
  local temp_file="$1"
  local account="$2"
  local timestamp
  timestamp="$(date +%s)"
  local pid="$$"
  local random
  random="$(od -An -N4 -tx4 </dev/urandom | tr -d ' ')"
  local queued_file="$QUEUE_DIR/${timestamp}-${pid}-${random}-${account}"

  if mv "$temp_file" "$queued_file"; then
    log_message "INFO" "Email queued for later delivery: $(basename "$queued_file")"
  else
    log_message "ERROR" "Failed to queue email"
    cleanup_temp_file "$temp_file"
    exit 1
  fi
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  main "$@"
fi