all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jean Louis <bugs@gnu.support>
To: Help GNU Emacs <help-gnu-emacs@gnu.org>
Subject: How I am handling msmtp queues
Date: Thu, 10 Mar 2022 09:23:45 +0300	[thread overview]
Message-ID: <courier.000000006229998E.000012AB@stw1.rcdrun.com> (raw)

I wish to describe how I use Emacs Lisp to send emails.

msmtp is about sending emails from computer:

Description     : A mini smtp client
URL             : https://marlam.de/msmtp/

For longer time I have used msmtp by cron job. However, that somehow
blocked itself, some colliding msmtp processes were in memory and I
had to kill them all before running msmtp-runqueue.sh script

Often I have thousands of emails in the queue, and sending them
reliably is important. Cron job for some unknown reasons did not do
well. 

Then I have made this Emacs Lisp function.

;;;; MSMTP

(defun msmtp-count-remaining ()
  "Count and send remaining MSMTP messages in the queue"
  (interactive)
  (let ((count (length (directory-files "~/.msmtpqueue" nil "\\.mail"))))
    (if (> count 0)
	(progn
	  (async-shell-command "msmtp-runqueue.sh" "*msmtp*")
	  (message "There is %s e-mails in queue." count))
      (message "No emails."))))

And I run the function with timer so that it is repeated every
minute. If there are some emails in the queue, the function will
dispatch them.

;; (run-with-timer 0 60 'msmtp-count-remaining)

Additionally I have adjusted the variable `display-buffer-alist' so
that buffers matching "msmtp" have function `display-buffer-no-window'
so that the buffer does not appear on screen when running the queue.

I can run the above Lisp with M-x msmtp-count-remaining or the timer
will run the script and send any messages in the queue.

And original msmtp script is here:

msmtp-runqueue.sh
=================

#!/usr/bin/env bash

QUEUEDIR="$HOME/.msmtpqueue"
LOCKFILE="$QUEUEDIR/.lock"
MAXWAIT=120

OPTIONS=$@

# wait for a lock that another instance has set
WAIT=0
while [ -e "$LOCKFILE" -a "$WAIT" -lt "$MAXWAIT" ]; do
	sleep 1
	WAIT="`expr "$WAIT" + 1`"
done
if [ -e "$LOCKFILE" ]; then
	echo "Cannot use $QUEUEDIR: waited $MAXWAIT seconds for"
	echo "lockfile $LOCKFILE to vanish, giving up."
	echo "If you are sure that no other instance of this script is"
	echo "running, then delete the lock file."
	exit 1
fi

# change into $QUEUEDIR 
cd "$QUEUEDIR" || exit 1

# check for empty queuedir
if [ "`echo *.mail`" = '*.mail' ]; then
	echo "No mails in $QUEUEDIR"
	exit 0
fi

# lock the $QUEUEDIR
touch "$LOCKFILE" || exit 1

# process all mails
for MAILFILE in *.mail; do
	MSMTPFILE="`echo $MAILFILE | sed -e 's/mail/msmtp/'`"
	echo "*** Sending $MAILFILE to `sed -e 's/^.*-- \(.*$\)/\1/' $MSMTPFILE` ..."
	if [ ! -f "$MSMTPFILE" ]; then
		echo "No corresponding file $MSMTPFILE found"
		echo "FAILURE"
		continue
	fi
	msmtp $OPTIONS `cat "$MSMTPFILE"` < "$MAILFILE"
	if [ $? -eq 0 ]; then
		rm "$MAILFILE" "$MSMTPFILE"
		echo "$MAILFILE sent successfully"
	else
		echo "FAILURE"
	fi
done

# remove the lock
rm -f "$LOCKFILE"

exit 0


Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



             reply	other threads:[~2022-03-10  6:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-10  6:23 Jean Louis [this message]
2022-03-10 16:21 ` How I am handling msmtp queues Felix Dietrich
2022-03-10 18:51   ` Jean Louis
2022-03-11 14:55     ` Felix Dietrich
2022-03-13 14:54       ` Jean Louis
2022-03-14 20:51       ` Tomas Hlavaty

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=courier.000000006229998E.000012AB@stw1.rcdrun.com \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.