Question

I'm writing a bash script to send me an email automatically. Mailx requires an EOT or ^D signal to know the message body is over and it can send. I don't want to hit ^D on the keyboard when I run script which is what it does now.

Here is my code:

#! /bin/bash
SUBJ="Testing"
TO="test@test.com"
MSG="message.txt"

echo "I am emailing you" >> $MSG
echo "Time: `date` " >> $MSG

mail -s "$SUBJ" -q "$MSG" "$TO"

rm -f message.txt
Was it helpful?

Solution

If you do not need to add more text and just need to send the content of $MSG, you can replace

mail -s "$SUBJ" -q "$MSG" "$TO"

with

mail -s "$SUBJ" "$TO" < "$MSG"

The EOT will be implicit in the < construct. -q is indeed only used to start a message. The rest is supposed to come through stdin.

OTHER TIPS

Pipe the output of a command group to mail.

#! /bin/bash
SUBJ="Testing"
TO="test@test.com"
MSG="message.txt"

{
  echo "I am emailing you"
  echo "Time: `date` "
} | mail -s "$SUBJ" -q "$MGS" "$TO"

rm -f message.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top