Search code examples
linuxemailsendmailmailx

Saving messages from mailx command line


Is there a way to save messages to a file using mailx, using only the command line? I know that I can copy messages to a file by first entering mailx:

mailx -A my_account

Then typing

& c 1-10 first_ten_messages.txt

Which would save the first 10 messages to a file.



What i'd like to do is something similar but without having the interactive part. So something like:

mailx -A my_account --options "c 1-10 first_ten_messages.txt"

Is this possible?

Thanks


Solution

  • this should do it.

    echo 'c 1-10 first_ten_messages.txt' | mailx -A my_account
    

    If you want to select messages from specific a specific sender, you can run a similar command:

    echo 'c from "Baji Boo" from_baji_boo.txt' | mailx -A my_account. 
    

    It is important to note that from works with the enveloped name, not straight email address.

    In general, running mailx and typing h will give you good information as well as reading man mailx.

    You can search messages in different ways and save to a file using the echo method.