How can I send multiple mailx commands in one line Bash command (e.g. read/print and delete)

573 Views Asked by At

I am testing some component that send email based on a specific logic.

In order to test this, I set up Postfix and Mailx in the Docker container where I run this component (the Docker container is not relevant for this question). The component will sent emails to (root or root@localhost)

I am trying now to write the test scripts that verify that emails were sent. For this I would like to use the mailx/mail utility in a bash script which will digest and clean up the received emails (based on some logic).

I know how to send one single mailx command:

echo "h" | mail -N

This will display the headers of the first message and quit.

I would like to delete the digested messages in the same command:

echo "<print_and_delete>" | mail -N

Is there a way to do this by passing a sequence of mailx commands?

An alternative would be to use:

echo "p" | mail -N; echo "d" | mail -N;

but I would like to know is there is a native mailx way to pipe/process multiple commands.

I would also like to use this in search and mark operations. For example, how can I do a search and continue with commands on the search results?

echo -e "f(text text_in_email)\n<???>" | mail -N

What should I replace <???> with in order to run commands on the search result (e.g. flag, mark as read, etc.)? Or to read the first email from the search results?

0

There are 0 best solutions below