12

I know we can use mail command in terminal to start using the mail program in interactive mode.

However, I want to read the email using mail command non-interactively.

Is there a command like

$ mail -optionToReadMail -mailNumber 1;

which will display the content of email in stdout ?

sps
  • 1,396
  • 5
  • 15
  • 23

1 Answers1

18

To print the first mail message in your default mailbox, use:

echo p | mail

mail is only interactive when stdin is a terminal. Because mail gets its stdin from a pipe, it is non-interactive. The p command (print) tells it to print the default (first) message.

For more options, you may find that man mail is very helpful.

John1024
  • 73,527
  • 11
  • 167
  • 163
  • Is there a way to do it without using pipe ?? – sps Oct 29 '15 at 00:56
  • 1
    @sps Sure. Use a here-string: `mail <<

    – John1024 Oct 29 '15 at 00:59
  • Is there a way without using the `<` character ? – sps Oct 29 '15 at 01:04
  • 2
    To help me answer that, please explain what you are actually trying to accomplish. Why are pipes and `<` unwanted? – John1024 Oct 29 '15 at 01:06
  • pipe and redirection operator are filtered ... it is a challenge that i am trying to solve... however as you have answered what i asked in original post i have marked it answered ... – sps Oct 29 '15 at 01:31
  • 3
    @sps In the future, put all your conditions in your question upfront, instead of adding them piecemeal after the question has been answered. – chepner Oct 29 '15 at 16:41
  • Do you know how to extract specific parts like the subject, from, to, body, etc...? – IMTheNachoMan May 07 '16 at 14:51
  • @IMTheNachoMan I'm sure that there are other ways but a simple one would be: `echo p | mail | grep ^Subject:` – John1024 May 07 '16 at 20:00