Browse Source

Added offline msmtp queuing

master
Micah Halter 1 month ago
parent
commit
5257e60520
4 changed files with 531 additions and 1 deletions
  1. 1
    0
      CHANGELOG.md
  2. 1
    1
      mutt/.config/mutt/sendmail
  3. 528
    0
      scripts/.config/Scripts/msmtpq
  4. 1
    0
      zsh/.zsh_aliases

+ 1
- 0
CHANGELOG.md View File

@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
9 9
 
10 10
 ### Added
11 11
 
12
+- Added offline msmtp email queuing
12 13
 - Added sending in background to `neomutt`
13 14
 - Created systemd service for compton
14 15
 - Added `i3blocks` configuration

+ 1
- 1
mutt/.config/mutt/sendmail View File

@@ -1,4 +1,4 @@
1 1
 #!/bin/bash
2 2
 
3 3
 ACCOUNT=$1
4
-$HOME/.config/Scripts/MIMEmbellish | msmtp -a "$1" "${@:2}"
4
+$HOME/.config/Scripts/MIMEmbellish | $HOME/.config/Scripts/msmtpq -a "$1" "${@:2}"

+ 528
- 0
scripts/.config/Scripts/msmtpq View File

@@ -0,0 +1,528 @@
1
+#!/usr/bin/env bash
2
+
3
+##--------------------------------------------------------------
4
+##
5
+##  msmtpq : queue funtions to both use & manage the msmtp queue,
6
+##             as it was defined by Martin Lambers
7
+##  Copyright (C) 2008 - 2015 Chris Gianniotis
8
+##
9
+##  This program is free software: you can redistribute it and/or modify
10
+##  it under the terms of the GNU General Public License as published by
11
+##  the Free Software Foundation, either version 3 of the License, or, at
12
+##  your option, any later version.
13
+##
14
+##--------------------------------------------------------------
15
+##
16
+## msmtpq is meant to be used by an email client - in 'sendmail' mode
17
+##   for this purpose, it is invoked directly as 'msmtpq'
18
+## it is also meant to be used to maintain the msmtp queue
19
+##   when it is evoked by the wrapper script 'msmtp-queue'
20
+##   (which calls this script as msmtpq --q-mgmt)
21
+##
22
+## there is a queue log file, distinct from the msmtp log,
23
+##   for all events & operations on the msmtp queue
24
+##   that is defined below
25
+##
26
+## (mutt users, using msmtpq in 'sendmail' mode,
27
+##  should make at least the following two settings in their .muttrc
28
+##    set sendmail = /path/to/msmtpq
29
+##    set sendmail_wait = -1
30
+##
31
+##  please see the msmtp man page and docs for further mutt settings
32
+##    and optimisations
33
+## )
34
+##
35
+## two essential patches by Philipp Hartwig
36
+## 19 Oct 2011 & 27 Oct 2011
37
+##
38
+##--------------------------------------------------------------
39
+## the msmtp queue contains unique filenames of the following form :
40
+##   two files for each mail in the queue
41
+##
42
+## creates new unique filenames of the form :
43
+##   MLF: ccyy-mm-dd-hh.mm.ss[-x].mail   -- mail file
44
+##   MSF: ccyy-mm-dd-hh.mm.ss[-x].msmtp  -- msmtp commands file
45
+## where x is a consecutive number only appended for uniqueness
46
+##   if more than one mail per second is sent
47
+##--------------------------------------------------------------
48
+
49
+
50
+dsp() { local L ; for L ; do [ -n "$L" ] && echo "  $L" || echo ; done ; }
51
+err() { dsp '' "$@" '' ; exit 1 ; }
52
+
53
+## ======================================================================================
54
+##      !!!          please define or confirm the following three vars           !!!
55
+##      !!!           before using the msmtpq or msmtp-queue scripts             !!!
56
+## ======================================================================================
57
+##
58
+## only if necessary (in unusual circumstances - e.g. embedded systems),
59
+##   enter the location of the msmtp executable  (no quotes !!)
60
+##   e.g. ( MSMTP=/path/to/msmtp )
61
+##   and uncomment the test for its existence
62
+MSMTP=msmtp
63
+#[ -x "$MSMTP" ] || \
64
+#  log -e 1 "msmtpq : can't find the msmtp executable [ $MSMTP ]"   # if not found - complain ; quit
65
+##
66
+## set the queue var to the location of the msmtp queue directory
67
+##   if the queue dir doesn't yet exist, create it (0700)
68
+##     before using this script
69
+##       e.g. ( mkdir msmtp.queue      )
70
+##            ( chmod 0700 msmtp.queue )
71
+##
72
+## the queue dir - modify this to reflect where you'd like it to be  (no quotes !!)
73
+Q=$XDG_CONFIG_HOME/msmtp/queue
74
+[ -d "$Q" ] || \
75
+  err '' "msmtpq : can't find msmtp queue directory [ $Q ]" ''     # if not present - complain ; quit
76
+##
77
+## set the queue log file var to the location of the msmtp queue log file
78
+##   where it is or where you'd like it to be
79
+##     ( note that the LOG setting could be the same as the )
80
+##     ( 'logfile' setting in .msmtprc - but there may be   )
81
+##     ( some advantage in keeping the two logs separate    )
82
+##   if you don't want the log at all unset (comment out) this var
83
+##     LOG=~/log/msmtp.queue.log  -->  #LOG=~/log/msmtp.queue.log
84
+##     (doing so would be inadvisable under most conditions, however)
85
+##
86
+## the queue log file - modify (or comment out) to taste  (but no quotes !!)
87
+LOG=~/.logs/msmtp.log
88
+## ======================================================================================
89
+
90
+## msmtpq can use the following environment variables :
91
+##   EMAIL_CONN_NOTEST   if set will suppress any testing for a connection
92
+##                         (the above var is deprecated & will be removed ; use the var below)
93
+##   EMAIL_CONN_TEST     if =x will suppress any testing for a connection
94
+##                       if =p or unset will use a ping test (debian.org) for a connection
95
+##                       if =P will use a fast ping test (8.8.8.8) for a connection
96
+##                       if =n will use netcat (nc) to test for a connection
97
+##                       if =s will use bash sockets to test for a connection
98
+##   EMAIL_QUEUE_QUIET   if set will cause suppression of messages and 'chatter'
99
+##                         (perhaps useful for some of the emacs mail clients)
100
+##
101
+## ======================================================================================
102
+##      !!!      define or confirm the following vars if you wish to set         !!!
103
+##      !!!      these properties here in the script - the same properties       !!!
104
+##      !!!      may be set externally, by means of environment variables        !!!
105
+##      !!!      note the internal variables, if set, will take precedence       !!!
106
+##      !!!      over properties set via environment variables                   !!!
107
+## ======================================================================================
108
+##
109
+#EMAIL_CONN_NOTEST=y                 # deprecated ; use below var
110
+#EMAIL_CONN_TEST={x| |p|P|n|s}       # see settings above for EMAIL_CONN_TEST
111
+EMAIL_CONN_TEST=P
112
+#EMAIL_QUEUE_QUIET=t
113
+## ======================================================================================
114
+
115
+umask 077                            # set secure permissions on created directories and files
116
+
117
+declare -i CNT                       # a count of mail(s) currently in the queue
118
+declare -a Q_LST                     # queue list array ; used selecting a mail (to send or remove)
119
+
120
+## do ; test this !
121
+#for sig in INT TERM EXIT; do
122
+#  trap "rm -f \"\$TMPFILE\"; [[ $sig == EXIT ]] || kill -$sig $$" $sig
123
+#done
124
+trap on_exit INT TERM EXIT           # run 'on_exit' on exit
125
+on_exit() {                          # unlock the queue on exit if the lock was set here
126
+  [ -n "$LKD" ] && lock_queue -u 2>/dev/null
127
+}
128
+
129
+#
130
+## ----------------------------------- functions common to both modes
131
+## ----------------------------------- (msmtpq & msmtp-queue)
132
+#
133
+
134
+## make an entry to the queue log file, possibly an error
135
+##   (log queue changes only ; not interactive chatter)
136
+## usage : log [ -e errcode ] msg [ msg ... ]
137
+##  opts : -e <exit code>  an error ; log msg & terminate w/prejudice
138
+## display msg to user, as well
139
+##
140
+log() {
141
+  local ARG RC PFX="$('date' +'%Y %d %b %H:%M:%S')"
142
+                                     # time stamp prefix - "2008 13 Mar 03:59:45 "
143
+  if [ "$1" = '-e' ] ; then          # there's an error exit code
144
+    RC="$2"                          # take it
145
+    shift 2                          # shift opt & its arg off
146
+  fi
147
+
148
+  [ -z "$EMAIL_QUEUE_QUIET" ] && dsp "$@"  # display msg to user, as well as logging it
149
+
150
+  if [ -n "$LOG" ] ; then            # log is defined and in use
151
+    for ARG ; do                     # each msg line out
152
+      [ -n "$ARG" ] && \
153
+        echo "$PFX : $ARG" >> "$LOG" # line has content ; send it to log
154
+    done
155
+  fi
156
+
157
+  if [ -n "$RC" ] ; then             # an error ; leave w/error return
158
+    [ -n "$LKD" ] && lock_queue -u   # unlock here (if locked)
159
+    [ -n "$LOG" ] && \
160
+      echo "    exit code = $RC" >> "$LOG" # logging ok ; send exit code to log
161
+    exit $RC                         # exit w/return code
162
+  fi
163
+}
164
+
165
+## write/remove queue lockfile for a queue op
166
+##
167
+lock_queue() {        # <-- '-u' to remove lockfile
168
+  local LOK="${Q}/.lock"             # lock file name
169
+  local -i MAX=240 SEC=0             # max seconds to gain a lock ; seconds waiting
170
+
171
+  if [ -z "$1" ] ; then              # lock queue
172
+    ## Philipp Hartwig patch #2
173
+    'mkdir' "$LOK" 2>/dev/null && LKD='t'
174
+    while [ -z "$LKD" ] && (( SEC < MAX )) ; do      # lock file present
175
+  	  sleep 1                                        # wait a second
176
+	    (( ++SEC ))                                    # accumulate seconds
177
+      'mkdir' "$LOK" 2>/dev/null && LKD='t'          # make lockdir ; lock queue ; set flag
178
+    done                                             # try again while locked for MAX secs
179
+    [ -z "$LKD" ] && \
180
+	    err '' "cannot use queue $Q : waited $MAX seconds for"\
181
+	           "  lockdir [ $LOK ] to vanish ; giving up"\
182
+	           'if you are certain that no other instance of this script'\
183
+	           "  is running, then 'rmdir' the lock dir manually" '' # lock file still there, give up
184
+
185
+  elif [ "$1" = '-u' ] ; then        # unlock queue
186
+    'rmdir' "$LOK"                   # remove the lock
187
+    unset LKD                        # unset flag
188
+  fi
189
+}
190
+
191
+## test whether system is connected
192
+## returns t/f (0/1)
193
+##
194
+connect_test() {
195
+  if [ -z "$EMAIL_CONN_TEST" ] || \
196
+     [ "$EMAIL_CONN_TEST" = 'p' ] ; then                       # use ping test (default)
197
+    # verify net connection - ping ip address of debian.org
198
+    # would ping -qnc2 -w4 be better ?
199
+    # would ping -qnc1 -w10 or -w20 be better ?
200
+    #ping -qnc1 -w4 debian.org >/dev/null 2>&1 || return 1
201
+    ping -qnc2 -w10 debian.org >/dev/null 2>&1 || return 1
202
+
203
+  elif [ "$EMAIL_CONN_TEST" = 'P' ] ; then                     # use quicker ping test
204
+    # I personally think that including a DNS lookup
205
+    # is a better connection test but some
206
+    # have found the above test too slow
207
+    ping -qnc1 -w4 8.8.8.8 >/dev/null 2>&1 || return 1
208
+
209
+  elif [ "$EMAIL_CONN_TEST" = 'n' ] ; then                     # use netcat (nc) test
210
+    # must, of course, have netcat (nc) installed
211
+    which nc >/dev/null 2>&1 || \
212
+      log -e 1 "msmtpq : can't find netcat executable [ nc ]"  # if not found - complain ; quit
213
+    'nc' -vz www.debian.org 80 >/dev/null 2>&1 || return 1
214
+
215
+  elif [ "$EMAIL_CONN_TEST" = 's' ] ; then                     # use sh sockets test
216
+    # note that this does not work on debian systems
217
+    #   where bash opened sockets are suppressed for security
218
+    #   reasons on multiuser systems - however, this should be
219
+    #   ok for single user systems (including embedded systems)
220
+    # test whether this is supported on your system before using...
221
+    # thank you to Brian Goose, on the list, for encouraging this
222
+    exec 3<>/dev/udp/debian.org/80 || return 1                 # open socket on site ; use dns
223
+    exec 3<&- ; exec 3>&-                                      # close socket
224
+  fi
225
+  return 0
226
+}
227
+
228
+#
229
+## ----------------------------------- functions for queue management
230
+## ----------------------------------- queue maintenance mode - (msmtp-queue)
231
+#
232
+
233
+## show queue maintenance functions
234
+##
235
+usage() {        # <-- error msg
236
+  dsp ''\
237
+      'usage : msmtp-queue functions' ''\
238
+      '        msmtp-queue < op >'\
239
+      '        ops : -r   run (flush) mail queue - all mail in queue'\
240
+      '              -R   send selected individual mail(s) in queue'\
241
+      '              -d   display (list) queue contents   (<-- default)'\
242
+      '              -p   purge individual mail(s) from queue'\
243
+      '              -a   purge all mail in queue'\
244
+      '              -h   this helpful blurt' ''\
245
+      '        ( one op only ; any others ignored )' ''
246
+  [ -z "$1" ] && exit 0 || { dsp "$@" '' ; exit 1 ; }
247
+}
248
+
249
+## get user [y/n] acknowledgement
250
+##
251
+ok() {
252
+  local R YN='Y/n'                   # default to yes
253
+
254
+  [ "$1" = '-n' ] && \
255
+    { YN='y/N' ; shift ; }           # default to no ; change prompt ; shift off spec
256
+
257
+  dsp "$@"
258
+  while true ; do
259
+    echo -n "  ok [${YN}] ..: "
260
+    read R
261
+    case $R in
262
+      y|Y) return 0 ;;
263
+      n|N) return 1 ;;
264
+      '')  [ "$YN" = 'Y/n' ] && return 0 || return 1 ;;
265
+      *)   echo 'yYnN<cr> only please' ;;
266
+    esac
267
+  done
268
+}
269
+
270
+## send a queued mail out via msmtp
271
+##
272
+send_queued_mail() {   # <-- mail id
273
+  local FQP="${Q}/${1}"              # fully qualified path name
274
+  local -i RC=0                      # for msmtp exit code
275
+
276
+  if [ -f "${FQP}.msmtp" ] ; then    # corresponding .msmtp file found
277
+    [ "$EMAIL_CONN_TEST" != 'x' ] && \
278
+    [ -z "$EMAIL_CONN_NOTEST" ] && { # do connection test
279
+      connect_test || {
280
+        log "mail [ $2 ] [ $1 ] from queue ; couldn't be sent - host not connected"
281
+        return
282
+      }
283
+    }
284
+
285
+    if $MSMTP $(< "${FQP}.msmtp") < "${FQP}.mail" ; then       # this mail goes out the door
286
+      log "mail [ $2 ] [ $1 ] from queue ; send was successful ; purged from queue"  # good news to user
287
+      'rm' -f ${FQP}.*                                         # nuke both queue mail files
288
+      ALT='t'                        # set queue changed flag
289
+    else                             # send was unsuccessful
290
+      RC=$?                          # take msmtp exit code
291
+      log "mail [ $2 ] [ $1 ] from queue ; send failed ; msmtp rc = $RC" # bad news ...
292
+    fi
293
+    return $RC                       # func returns exit code
294
+  else                               # corresponding MSF file not found
295
+    log "preparing to send .mail file [ $1 ] [ ${FQP}.mail ] but"\
296
+        "  corresponding .msmtp file [ ${FQP}.msmtp ] was not found in queue"\
297
+        '  skipping this mail ; this is worth looking into'    # give user the bad news
298
+  fi                                                           # (but allow continuation)
299
+}
300
+
301
+## run (flush) queue
302
+##
303
+run_queue() {    # <- 'sm' mode      # run queue
304
+  local M LST="$('ls' $Q/*.mail 2>/dev/null)"            # list of mails in queue
305
+  local -i NDX=0
306
+
307
+  if [ -n "$LST" ] ; then            # something in queue
308
+    for M in $LST ; do               # process all mails
309
+      ((NDX++))
310
+      send_queued_mail "$(basename $M .mail)" "$NDX"     # send mail - pass {id} only
311
+    done
312
+  else                               # queue is empty
313
+    [ -z "$1" ] && \
314
+      dsp '' 'mail queue is empty (nothing to send)' ''
315
+  fi                                 # inform user (if not running in sendmail mode)
316
+}
317
+
318
+## display queue contents
319
+##
320
+display_queue() {      # <-- { 'purge' | 'send' } (op label) ; { 'rec' } (record array of mail ids)
321
+  local M ID LST="$('ls' ${Q}/*.mail 2>/dev/null)"       # list of mail ids in queue
322
+
323
+  CNT=0
324
+  if [ -n "$LST" ] ; then            # list has contents (any mails in queue)
325
+    for M in $LST ; do               # cycle through each
326
+      ID="$(basename $M .mail)"      # take mail id from filename
327
+      ((CNT++))
328
+      dsp '' "mail  num=[ $CNT ]  id=[ $ID ]"                  # show mail id ## patch in
329
+      'egrep' -s --colour -h '(^From:|^To:|^Subject:)' "$M"    # show mail info
330
+      [ -n "$2" ] && Q_LST[$CNT]="$ID" # bang mail id into array (note 1-based array indexing)
331
+    done
332
+    echo
333
+  else                               # no mails ; no contents
334
+    [ -z "$1" ] && \
335
+      dsp '' 'no mail in queue' '' || \
336
+      dsp '' "mail queue is empty (nothing to $1)" ''    # inform user
337
+    exit 0
338
+  fi
339
+}
340
+
341
+## delete all mail in queue, after confirmation
342
+##
343
+purge_queue() {
344
+  display_queue 'purge'              # show queue contents
345
+  if ok -n 'remove (purge) all mail from the queue' ; then
346
+    lock_queue                       # lock here
347
+    'rm' -f "$Q"/*.*
348
+    log 'msmtp queue purged (all mail)'
349
+    lock_queue -u                    # unlock here
350
+  else
351
+    dsp '' 'nothing done ; queue is untouched' ''
352
+  fi
353
+}
354
+
355
+## select a single mail from queue ; delete it or send it
356
+## select by mail index (position in queue) or mail id
357
+##
358
+select_mail() {  # <-- < 'purge' | 'send' >
359
+  local OK ID                                        # mail id
360
+  local -i I NDX                                     # mail index (position in queue)
361
+
362
+  while true ; do                                    # purge an individual mail from queue
363
+    display_queue "$1" 'rec'                         # show queue contents ; make mail ids array
364
+
365
+    ## allow selection also by mail index
366
+    if [ $CNT -eq 1 ] ; then                         # only one mail in queue ; take its id
367
+      NDX=1
368
+      ID="${Q_LST[1]}"
369
+    else                                             # more than one mail ; select its id
370
+      while true ; do                                # get mail id
371
+        OK='t'                                       # optimistic to a fault
372
+        dsp "enter mail number or id to $1"          # <-- num or file name (only, no suff)
373
+        echo -n '    ( <cr> alone to exit ) ..: '
374
+        read ID
375
+        [ -n "$ID" ] || return                       # entry made - or say good bye
376
+
377
+        if [ "${ID:4:1}" != '-' ] ; then             # mail id *not* entered ; test index num
378
+          for (( I=0 ; I<${#ID} ; I++ )) ; do        # test index number
379
+            if [[ "${ID:${I}:1}" < '0' || \
380
+                  "${ID:${I}:1}" > '9' ]] ; then
381
+              dsp '' "[ $ID ] is neither a valid mail id"\
382
+                     'nor a valid mail number' ''
383
+              unset OK
384
+            fi
385
+          done
386
+          [ -z "$OK" ] && continue                   # format not ok (not all nums)
387
+
388
+          NDX=$ID
389
+          if [ $NDX -lt 1 ] || [ $NDX -gt $CNT ] ; then  # test number range (1 - $CNT)
390
+            dsp '' "[ $NDX ] is out of range as a mail number"\
391
+                   "validity is from 1 to $CNT"
392
+            continue                                 # try again
393
+          fi
394
+
395
+          ID="${Q_LST[$NDX]}"                        # format & range were ok ; use it
396
+          break                                      # valid mail selection
397
+        else                                         # mail id entered
398
+          for (( NDX=1 ; NDX<=${#Q_LST[*]} ; NDX++ )) ; do # find entered id in queue list
399
+            [ "$ID" = "${Q_LST[$NDX]}" ] && break
400
+          done
401
+          [ $NDX -le ${#Q_LST[*]} ] && \
402
+            break || \
403
+            dsp '' "mail [ $ID ] not found ; invalid id" # mail selection valid (found) or
404
+        fi                                               # fell through (not found) complain
405
+      done                                               # and ask again
406
+    fi
407
+
408
+    if ok '' "$1 :"\
409
+          "  mail num=[ $NDX ]"\
410
+          "        id=[ $ID ]" '' ; then             # confirm mail op
411
+      if [ "$1" = 'purge' ] ; then                   # purging
412
+        lock_queue                                   # lock here
413
+        'rm' -f "$Q"/"$ID".*                         # msmtp - nukes single mail (both files) in queue
414
+        log "mail [ $ID ] purged from queue"         # log op
415
+        lock_queue -u                                # unlock here
416
+        ALT='t'                                      # mark that a queue alteration has taken place
417
+      else                                           # sending
418
+        lock_queue                                   # lock here
419
+        send_queued_mail "$ID" "$NDX"                # send out the mail
420
+        lock_queue -u                                # unlock here
421
+      fi
422
+    else                                             # user opts out
423
+      dsp '' 'nothing done to this queued email'     # soothe user
424
+      [ $CNT -eq 1 ] && break                        # single mail ; user opted out
425
+    fi
426
+    dsp '' "--------------------------------------------------"
427
+  done
428
+
429
+  if [ -n "$ALT" ] ; then            # queue was changed
430
+    dsp '' 'done' ''
431
+  else                               # queue is untouched
432
+    dsp '' 'nothing done ; queue is untouched' ''
433
+  fi
434
+}
435
+
436
+#
437
+## ----------------------------------- functions for directly sending mail
438
+## ----------------------------------- 'sendmail' mode - (msmtpq)
439
+#
440
+
441
+## ('sendmail' mode only)
442
+## make base filename id for queue
443
+##
444
+make_id() {
445
+  local -i INC                       # increment counter for (possible) base fqp name collision
446
+
447
+  ID="$(date +%Y-%m-%d-%H.%M.%S)"    # make filename id for queue    (global
448
+  FQP="${Q}/$ID"                     # make fully qualified pathname  vars)
449
+  ## Philipp Hartwig patch #1
450
+  if [ -f "${FQP}.mail" -o -f "${FQP}.msmtp" ] ; then    # ensure fqp name is unique
451
+    INC=1                            # initial increment
452
+	  while [ -f "${FQP}-${INC}.mail" -o -f "${FQP}-${INC}.msmtp" ] ; do # fqp name w/incr exists
453
+      (( ++INC ))                    # bump increment
454
+	  done
455
+	  ID="${ID}-${INC}"                # unique ; set id
456
+	  FQP="${FQP}-${INC}"              # unique ; set fqp name
457
+  fi
458
+}
459
+
460
+## ('sendmail' mode only)
461
+## enqueue a mail
462
+##
463
+enqueue_mail() { # <-- all mail args ; mail text via TMP
464
+  if echo "$@" > "${FQP}.msmtp" ; then     # write msmtp command line to queue .msmtp file
465
+    log "enqueued mail as : [ $ID ] ( $* ) : successful" # (queue .mail file is already there)
466
+  else                                     # write failed ; bomb
467
+    log -e "$?" "queueing - writing msmtp cmd line { $* }"\
468
+                "           to [ ${ID}.msmtp ] : failed"
469
+  fi
470
+}
471
+
472
+## ('sendmail' mode only)
473
+## send a mail (if possible, otherwise enqueue it)
474
+## if send is successful, msmtp will also log it (if logging enabled in ~/.msmtprc)
475
+##
476
+send_mail() {    # <-- all mail args ; mail text via TMP
477
+  [ "$EMAIL_CONN_TEST" != 'x' ] && \
478
+  [ -z "$EMAIL_CONN_NOTEST" ] && {   # do connection test
479
+    connect_test || {
480
+      log "mail for [ $* ] : couldn't be sent - host not connected"
481
+      enqueue_mail "$@"              # enqueue the mail
482
+      return
483
+    }
484
+  }
485
+
486
+  if $MSMTP "$@" < "${FQP}.mail" > /dev/null ; then      # send mail using queue .mail fil
487
+    log "mail for [ $* ] : send was successful"          # log it
488
+    'rm' -f ${FQP}.*                 # remove all queue mail files .mail & .msmtp file
489
+    run_queue 'sm'                   # run/flush any other mails in queue
490
+  else                               # send failed - the mail stays in the queue
491
+    log "mail for [ $* ] : send was unsuccessful ; msmtp exit code was $?"\
492
+        "enqueued mail as : [ $ID ] ( $* )"    # (queue .mail file is already there)
493
+  fi
494
+}
495
+
496
+#
497
+## -- entry point
498
+#
499
+
500
+if [ ! "$1" = '--q-mgmt' ] ; then    # msmtpq - sendmail mode
501
+  lock_queue                         # lock here
502
+  make_id                            # make base queue filename id for this mail
503
+  # write mail body text to queue .mail file
504
+  cat > "${FQP}.mail" || \
505
+    log -e "$?" "creating mail body file [ ${FQP}.mail ] : failed" # test for error
506
+  # write msmtp command line to queue .msmtp file
507
+  echo "$@" > "${FQP}.msmtp" || \
508
+    log -e "$?" "creating msmtp cmd line file { $* }"\
509
+                "           to [ ${ID}.msmtp ] : failed" # test for error
510
+  send_mail "$@"                     # send the mail if possible, queue it if not
511
+  lock_queue -u                      # unlock here
512
+else                                 # msmtp-queue - queue management mode
513
+  shift                              # trim off first (--q-mgmt) arg
514
+  OP=${1:1}                          # trim off first char of OP arg
515
+  case "$OP" in                      # sort ops ; run according to spec
516
+    r)    lock_queue
517
+          run_queue
518
+          lock_queue -u       ;;     # run (flush) the queue
519
+    R)    select_mail send    ;;     # send individual mail(s) in queue
520
+    d|'') display_queue       ;;     # display (list) all mail in queue (default)
521
+    p)    select_mail purge   ;;     # purge individual mail(s) from queue
522
+    a)    purge_queue         ;;     # purge all mail in queue
523
+    h)    usage               ;;     # show help
524
+    *)    usage "[ -$OP ] is an unknown msmtp-queue option" ;;
525
+  esac
526
+fi
527
+
528
+exit 0

+ 1
- 0
zsh/.zsh_aliases View File

@@ -41,6 +41,7 @@ wt()
41 41
 
42 42
 # email aliases
43 43
 alias m='neomutt'
44
+alias msmtpqm='msmtpq --q-mgmt'
44 45
 # calendar aliases
45 46
 alias kl='khal'
46 47
 alias ikl='ikhal'

Loading…
Cancel
Save