Search code examples
bashemailmailxeot

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

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.