Putting subject & from in mail from command line

I couldn’t figure out how to send an email with both the subject and from set from the command line.

  1. mail -s "my subject" example@foo.bar

worked with mail but would only set the subject.

  1. sendmail -f "sender@foo.bar" receiver@foo.bar

worked with sendmail but would only set the “from”. With a little help from Google I put this together:

  1. #!/bin/bash
  2.  
  3. HOST=`hostname | awk 'BEGIN { FS="." } ; {print $1}'`
  4. DOMAIN=`hostname`
  5.  
  6. PASSWORD='mypassword'
  7.  
  8. BODY=`tail -n 40 /var/log/maillog`
  9.  
  10. FROM="$HOST@$DOMAIN"
  11. TO='receiver@foo.bar'
  12. SUBJECT="Last 40 lines of maillog"
  13.  
  14. (
  15. echo "From: $FROM"
  16. echo "Subject: $SUBJECT"
  17. echo "To: $TO"
  18. echo "$BODY"
  19. ) | sendmail -f $FROM $TO