2

I have a situation where I need to delete from mailbox (eg. /var/mail/root) messages with specific Message-Id.

Following code works only from console, but I have to do it without user interaction running from cron /etc/crontab.

File: /tmp/clear_spam_test

mutt -f /var/mail/root -e "set alias_file=/var/mail/root" -e "set crypt_use_gpgme=no" -e "push <delete-pattern>~iSomeMessageId@messageid.test\n<sync-mailbox>qy"

I tried many variations

  1. ssh -tt localhost 'bash -s' < /tmp/clear_spam_test

Output:
mutt -f /var/mail/root -e "set alias_file=/var/mail/root" -e "set crypt_use_gpgme=no" -e "push <delete-pattern\>~iSomeMessageId@messageid.test\n\<sync-mailbox\>qy"

echo -e "\nTEST $( whoami ) $0"
exit 0
<n>~iSomeMessageId@messageid.test\n<sync-mailbox>qy"
Error opening terminal: unknown.
TEST root bash
exit
Connection to localhost closed.

  1. ssh -t localhost 'bash -s' < /tmp/clear_spam_test

Output:
Pseudo-terminal will not be allocated because stdin is not a terminal.
No recipients were specified.

  1. ssh -T localhost 'bash -s' < /tmp/clear_spam_test

Output:
No recipients were specified.

  1. ssh -tt $server <<'ENDSSH' echo $(/tmp/clear_spam_test) exit 0 ENDSSH

Output:
Error opening terminal: unknown.
TEST root /tmp/clear_spam_test
logout
Connection to localhost closed.

  1. ssh -t $server <<'ENDSSH' echo $(/tmp/clear_spam_test) exit 0 ENDSSH

Output:
Pseudo-terminal will not be allocated because stdin is not a terminal.
mesg: ttyname failed: Inappropriate ioctl for device
No recipients were specified.
TEST root /tmp/clear_spam_test

  1. ssh -T $server <<'ENDSSH' echo $(/tmp/clear_spam_test) exit 0 ENDSSH

Output:
mesg: ttyname failed: Inappropriate ioctl for device
No recipients were specified.
TEST root /tmp/clear_spam_test

None of it works. I also tried IFS.

  • I still have`t figured it out. Any clue? – Pauli Jun 6 '19 at 8:58
  • Is it fair game to shut down the local MTA while the delete operation occurs? Then the /var/mail/root file could be backed up for safety, and edited directly via a script. See the man page for formail, a component of the procmail family, and note the -D maxlen idcache command-line switch. – Jim L. Jan 7 at 0:39
3

Same problem here. This mutt command seems to depend on a working terminal window that cron cannot build. At least for me it helped to start a virtual terminal using screen:

screen -d -m  mutt -f /var/mail/root -e "set alias_file=/var/mail/root" -e "set crypt_use_gpgme=no" -e "push <delete-pattern>~iSomeMessageId@messageid.test\n<sync-mailbox>qy"
| improve this answer | |
  • Thank you very much. The screen command did the work. I only had to change the line to: screen -d -m mutt -f /var/mail/root -e "set alias_file=/var/mail/root" -e "set crypt_use_gpgme=no" -e "push ** D**~iSomeMessageId@messageid.test\n<sync-mailbox>qyqy" and add another qy to the end, so it does properly closes the mutt. – Pauli Jan 7 at 13:01
  • The D~i option deletes only message with matching pattern. – Pauli Jan 7 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.