Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Maybe some folks here with a better understanding of pipes than myself can help me figure out how to do the following using pipes and not a temporary file. I need a minimalist method to format a couple of email headers, append an email body, then send that email with `msmtp`. This is all running on a small embedded linux system with some additional packages bolted on. This will be mailing the status updates from my backup server to myself. Partly I'm doing this because I don't want to run a full-blown mail user agent, and partly I'm just curious how concise it can be :)

The requirement is that this be an executable script which can take any number of command-line arguments describing email headers, and which receives the body of an email on STDIN. I think this can all be done via pipes but I'm not entirely sure how - the trick is that the output of the program over STDOUT needs to be the concatenation of formatted command-line arguments plus the contents of STDIN. I had difficulty figuring out how to have two different commands both send output, in order, to the STDIN of `msmtp -t`. msmtp is configured to send email via a Google Apps account.

Here's what I'm using at the moment. It works but I think it could be more elegant and UNIXY without the temporary file.

  #!/bin/sh
  T=`/opt/bin/mktemp`                         # create a temporary file
  echo -e "From:$1\nTo:$2\nSubject:$3\n" > $T # format command-line args into the file
  cat <&0 >>$T                                # append STDIN to the file
  msmtp -t <$T                                # mail the file (-t parses the smtp recipient from the email headers)
  rm $T                                       # delete the file

  # used as follows:
  #/usr/local/bin/minimail "sender@example.com" "recipient@example.com" "Subject of the Email" <body.txt
Any ideas?


    (echo;cat)|msmtp
But yeah, stackoverflow.com


That works perfectly. Thank you.

Sorry for the noise.





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: