hi,
can anyone give me a brief example using mailx and heredoc to send an email?

#!/bin/sh
mail -s "subject" target@address >>body
hello all,
          this is supposed to be the body of the mail
body

-----------------------------------------> i think i need somethink like above code, with mailx instead of mail -s......... please correct me

Recommended Answers

All 3 Replies

Hey There,

You should be good to go with just a little change-up:

cat <<EOF | mailx -s "subject" target@address
hello all,
this is supposed to be the body of the mail
EOF

Hope that helps you out :)

, Mike

Hey There,

You should be good to go with just a little change-up:

Hope that helps you out :)

, Mike

Mike,
thanks again, and it surely works. Would you explain a little bit of what you have done?

cat <<EOF | mailx -s "subject" target@address
is it like, you cat whatever in the new file EOF which contains the "mailx -s ..... " does the | works just like the regular | does? thanks

Hey, no problem, glad I could help out :)

The syntax of this is a little confusing, even to me if I just give it a quick glance. But so many things with Linux/Unix are until you get used to them ;)

Basically, I'm catting everything up to the EOF delimiter (which could have been called anything like END or something) and piping all that to mailx, like a regular pipe (|)

The confusing part is the input/output on the same line:

cat <<EOF |mailx -s blah@blah.com

It's equivalent to sending output to a file, which looks like it makes more sense, even though it's the exact same principle ;)

cat << EOF >OUTFILE
sometext
EOF

except instead of dumping the output from after the cat command up to EOF into a file, we're piping it to mailx.

Does that make sense? When you've done it enough times it seems harder to explain it ;)

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.