blob: fb1e4874bb2be6df04c7956f53b1b199d7573c0a (
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
#!/usr/bin/env bash
# msmtp queue management utility
#
# Provides commands to process, check status, and clear the
# msmtp email queue that is populated by msmtp-send.
set -euo pipefail
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}"
QUEUE_DIR="${DATA_DIR}/msmtp-queue"
LOG_FILE="${DATA_DIR}/msmtp-queue-processor.log"
MSMTP_PATH="$(command -v msmtp)"
main() {
if [[ $# -eq 0 ]]; then
print_usage
exit 1
fi
local subcommand="$1"
shift
case "$subcommand" in
process)
process_queue "$@"
;;
status)
show_status "$@"
;;
clear)
clear_queue "$@"
;;
-h | --help)
print_usage
exit 0
;;
*)
log_message "ERROR" "Unknown subcommand: $subcommand"
print_usage
exit 1
;;
esac
}
print_usage() {
cat <<USAGE
Usage: $(basename "$0") <subcommand> [OPTIONS]
A utility to manage the msmtp email queue.
SUBCOMMANDS:
process Process all queued emails (attempt to send)
status Show queue status and recent activity
clear [ACCOUNT] Clear queued emails (all accounts or specific account)
OPTIONS:
-h, --help Show this help message
EXAMPLES:
$(basename "$0") process
$(basename "$0") status
$(basename "$0") clear
$(basename "$0") clear gmail
NOTES:
- Queue directory: $QUEUE_DIR
- Log file: $LOG_FILE
- Queued files are named: timestamp-pid-random-ACCOUNT
USAGE
}
log_message() {
local level="$1"
shift
local message
message="$(date '+%Y-%m-%d %H:%M:%S') [$level] $*"
if [[ -d "$QUEUE_DIR" ]]; then
echo "$message" >>"$LOG_FILE"
fi
echo "$message" >&2
}
process_queue() {
if [[ $# -gt 0 ]]; then
log_message "ERROR" "process subcommand takes no arguments"
print_usage
exit 1
fi
if ! command -v "$MSMTP_PATH" >/dev/null 2>&1; then
log_message "ERROR" "msmtp not found at $MSMTP_PATH"
exit 1
fi
setup_queue_directory
local total_count
total_count="$(count_queued_files)"
if [[ "$total_count" -eq 0 ]]; then
log_message "INFO" "No emails in queue"
return 0
fi
log_message "DEBUG" "Files from get_queued_files:"
get_queued_files | while IFS= read -r debug_file; do
log_message "DEBUG" " - $debug_file"
done
log_message "INFO" "Processing $total_count queued emails"
local sent_count=0
local failed_count=0
# NOTE: Temporarily disable exit on error for msmtp call
# so that it can continue processing other files even if
# one fails.
set +e
while IFS= read -r file; do
if [[ -n "$file" ]]; then
local account
account="$(extract_account_from_filename "$file")"
log_message "INFO" "Attempting to send $(basename "$file") via $account"
local msmtp_output
if msmtp_output=$("$MSMTP_PATH" --account="$account" -t <"$file" 2>&1); then
rm "$file"
log_message "INFO" "SUCCESS: Sent $(basename "$file") via $account"
((sent_count++))
else
log_message "ERROR" "FAILED: Could not send $(basename "$file") via $account - $msmtp_output"
((failed_count++))
fi
fi
done < <(get_queued_files)
set -e
log_message "INFO" "Processing complete: $sent_count sent, $failed_count failed"
}
setup_queue_directory() {
if ! mkdir -p "$QUEUE_DIR"; then
log_message "ERROR" "Failed to create queue directory: $QUEUE_DIR"
exit 1
fi
}
extract_account_from_filename() {
local filename="$1"
local basename_file
basename_file="$(basename "$filename")"
# NOTE: Expected format of timestamp-pid-random-ACCOUNT
if [[ "$basename_file" =~ ^[0-9]+-[0-9]+-[a-fA-F0-9]+-(.+)$ ]]; then
echo "${BASH_REMATCH[1]}"
else
log_message "WARN" "Could not extract account from filename: $basename_file"
echo "unknown"
fi
}
get_queued_files() {
local account_filter="${1:-}"
if [[ ! -d "$QUEUE_DIR" ]]; then
return 0
fi
for file in "$QUEUE_DIR"/*; do
if [[ -f "$file" ]]; then
if [[ -z "$account_filter" ]]; then
echo "$file"
else
local file_account
file_account="$(extract_account_from_filename "$file")"
if [[ "$file_account" == "$account_filter" ]]; then
echo "$file"
fi
fi
fi
done
}
count_queued_files() {
local account_filter="${1:-}"
local count=0
while IFS= read -r -d '' file; do
((count++))
done < <(get_queued_files "$account_filter" | tr '\n' '\0')
echo "$count"
}
show_status() {
if [[ $# -gt 0 ]]; then
log_message "ERROR" "status subcommand takes no arguments"
print_usage
exit 1
fi
setup_queue_directory
echo "=== Queue Status ==="
local total_count
total_count="$(count_queued_files)"
echo "Total queued: $total_count"
if [[ "$total_count" -gt 0 ]]; then
echo ""
echo "By account:"
while IFS= read -r account; do
if [[ -n "$account" ]]; then
local account_count
account_count="$(count_queued_files "$account")"
echo " $account: $account_count"
fi
done < <(get_all_accounts)
fi
if [[ -f "$LOG_FILE" ]]; then
echo ""
echo "=== Recent Activity (last 10 lines) ==="
tail -10 "$LOG_FILE" 2>/dev/null || echo "No log entries yet"
fi
}
get_all_accounts() {
local accounts=()
while IFS= read -r file; do
if [[ -n "$file" ]]; then
local account
account="$(extract_account_from_filename "$file")"
if [[ "$account" != "unknown" ]]; then
accounts+=("$account")
fi
fi
done < <(get_queued_files)
# Remove duplicates and sort
printf '%s\n' "${accounts[@]}" | sort -u
}
clear_queue() {
local account_filter="${1:-}"
if [[ $# -gt 1 ]]; then
log_message "ERROR" "clear subcommand takes at most one argument (account name)"
print_usage
exit 1
fi
setup_queue_directory
if [[ -n "$account_filter" ]]; then
local account_count
account_count="$(count_queued_files "$account_filter")"
if [[ "$account_count" -eq 0 ]]; then
echo "No queued emails for account: $account_filter"
return 0
fi
while IFS= read -r file; do
if [[ -n "$file" ]]; then
rm "$file"
fi
done < <(get_queued_files "$account_filter")
log_message "INFO" "Cleared $account_count queued emails for account: $account_filter"
echo "Cleared $account_count queued emails for account: $account_filter"
else
local total_count
total_count="$(count_queued_files)"
if [[ "$total_count" -eq 0 ]]; then
echo "No queued emails to clear"
return 0
fi
while IFS= read -r file; do
if [[ -n "$file" ]]; then
rm "$file"
fi
done < <(get_queued_files)
log_message "INFO" "Cleared $total_count queued emails (all accounts)"
echo "Cleared $total_count queued emails (all accounts)"
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
|