简体   繁体   中英

Can't figure out how to send ^D (EOT) signal to mailx in bash script

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM