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

2

3

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="[email protected]"
MSG="message.txt"

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

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

rm -f message.txt
Superhuman answered 5/11, 2013 at 20:47 Comment(1)
How do you want to tell mailx you finished your body without typing ^D?Voyles
P
5

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.

Pigeonhole answered 5/11, 2013 at 20:55 Comment(5)
MSG is the name of a file, not a string containing the message body. You want <, not <<<.Cookshop
That worked with chepner's '<' use (since i was using a file)! Appreciate it. Both of you.Superhuman
@Cookshop Indeed. I was fooled by the variable name.Pigeonhole
mail -s "$SUBJ" -q "$MSG" "$TO" < /dev/null should work as well.Godred
@user2957985 protip: use mktemp to create your temporary filenames to ensure you're not clobbering files that you don't mean to clobber.Grizzle
C
2

Pipe the output of a command group to mail.

#! /bin/bash
SUBJ="Testing"
TO="[email protected]"
MSG="message.txt"

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

rm -f message.txt
Cookshop answered 5/11, 2013 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.