Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending Mail /w Command Line

Status
Not open for further replies.

ilovelinux2006

Programmer
Jun 6, 2006
32
US
Hello everyone,

I am trying to send an email in a command line.

Something like

mail mail@website.com -s Subject -m message here?


I've been looking up the command for a long time, but cannot figure it out. Maybe even using the command mutt? Thanks!!!
 
You need to pass the message contents in through standard input. A "here" document is ideal for this sort of thing:

Code:
mail -s "subject" someone@example.com << HERE
Dear Sir/Madam,

I am a message.

Kind regards,

Your system.
HERE

Annihilannic.
 
Ok that works great. Im trying to add this script with another script. Just at the end it wont let me finish the command it still has the

>
>
>

Im trying to add a ;done, but in front of the message >;done it doesnt execute. How do I get out of the

>
>
>

Thanks!
 
It sounds like you haven't terminated something. If it's the "here" document it needs to end in the keyword you specified, such as "HERE" in my example. That needs to be on the very beginning of a line.

Also it's possible that you didn't terminate a pair of quotes.

Annihilannic.
 
Thanks Annihilannic, This is the code:

for u in $(</var/log/script/user.txt); do mail -s "Planned Maintenance Outage" $u << HERE
Dear Sir/Madam,

We will be updating our services on Wednesday, Thursday, and Friday between 11:00pm & 7:00am. Planned maintenance outage for all customers..
HERE

;done

but it comes out

for u in $(</var/log/script/user.txt); do mail -s "Planned Maintenance Outage" $u << HERE
>Dear Sir/Madam,
>
>We will be updating our service.
>HERE
>
>;done
 
I just tested the command and Annihilannic is right put the work HERE as the last item in your email and it closes the email and sends it. Nice code....
 
Hmm... I can't see anything wrong with that code. Are there any funny characters in your user.txt?

Annihilannic.
 
I tested it again using the following and it does fail.

mail -s test name@test.com < test.file << HERE

That is what is being done. Its reading the HERE as a new name to send to.

What fixed it is this
mail -s test name@test.com <test.file <<HERE^JHERE

That works.
 
I tried

mail -s test name@test.com <test.file <<HERE^JHERE

it doesnt execute the command, it gives the

>
>
after
 
Sorry I'm not sure how I got the ^J to work. When I typed the folowing command

mail -s test name@test.com <test.file <<HERE

it gave me a > I then typed HERE and it worked. When I recalled the command I saw it had added the ^JHERE to the command. I cut a pasted it into a new command and it worked too. The ^J is a esc character or something. I typed the command and it failed to. But a cut a paste worked. Maybe some one can tell us what the ^J exc character is.
 
^J is the LF character (hex 0A) i.e. the standard EndOfLine.

Anyway using < AND << for the same command has no meaning for me.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Just take the ; out in front of the ;done and I think it will work fine.

Annihilannic.
 
Try either this:
for u in $(</var/log/script/user.txt); do
mail -s "Planned Maintenance Outage" $u << HERE
Dear Sir/Madam,

We will be updating our services on Wednesday, Thursday, and Friday between 11:00pm & 7:00am. Planned maintenance outage for all customers..
HERE
done

Or this:
for u in $(</var/log/script/user.txt); do
echo "Dear Sir/Madam,

We will be updating our services on Wednesday, Thursday, and Friday between 11:00pm & 7:00am. Planned maintenance outage for all customers..
" | mail -s "Planned Maintenance Outage" $u
done


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top