How can i send mail to multiple addresses in same domain in bash?

Suppose i have a txt file that is the list of the addresses,something like:

lala0045 john james
lala0234 george james

and i want to send an email to lala0045@blabla.com and lala0234@blabla.com,the same domain...what is the exact syntax i should use in my script?
there is a command system(???) but i dont know how to use that...
i have tried a syntax i found using mail,mailx,mailto but no result occured...
any ideas?

Please try:

mail -s "<subject>" "<delivery list>" "matter"

example:

mail -s "my subject" "id1@c.com id2@c.com id3@c.com" "Hey! i m mailing u all"

try this as hard coded first, then v ll get ur address automatically. Check if the mail comes. Currently m not on a UNIX system so u ll have to try it and let me know if it worked for u unless somebody else provides the complete solution.

Regards,
Hkansal

A really dirty solution but will get you going is :-

#!/bin/bash

file="inputfile"
domain="blahblah.com"

for local in $(awk '{print $1}' <$file)
do
        address_list="$address_list $local@$domain"
done

echo "address list is [$address_list]"

echo "Hello this is body of the email" | mailx -s "Subject" $address_list


Obviously that doesn't do any variable checking or anything.

If you want to code it with checks you may find this reg ex useful i spent ages doing the other day to validate a space separated email list. Just be aware its not totally rfc compliant as only accepts -_ as special characters and not the full range you should.

if [ -n $(echo $address_list | awk '/^(\ )*([A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)*@([A-Za-z0-9_-]+\.)+[A-Za-z]+((\ )+[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)*@([A-Za-z0-9_-]+\.)+[A-Za-z]+)*)(\ )*$/ {print}')" ];then
       # Valid email list
       mailx -s "subject" $address_list < ./email_contents
else
       # Email list problem
       echo "WARNING: Email address list problem [ $address_list ]"
       ....other stuff....
fi

You can use a mail alias in /etc/aliases, then just send the mail to the alias:

 # cat /etc/aliases
 maillist: user1@blah.com,user2@blah.com,user3@blah.com

When you send mail to maillist, user1, user2, and user3 will all get it.

Quick edit: maillist can be any name.

thank you very much for your answers.i wanted to tell you that i have a bunch of files that contain the first half of the address(that is without the @domain part)so ,each time one of these files is opened, a different first part is read.the domain is the only thing that remains the same.so i need the syntax of the mail command that reads addresses from the fisrt column of a file and then attaches the '@domain' part.thank you again very much;)

//my mistake.didn't have mail installed on my ubuntu.so avascript i tried yours which is closer to what i want to do,but although it doesn't have any mistake i see no email on my mailbox(i send to my self in that domain in order to check the algorithm)...

That may be your mail gateway or server mail settings.

Just try this to start with from command line before any scripting.

# echo "Mail test" | mailx -s "Mail Subject" <your_email@address.com>

Obviously removing the < > brackets.

Try typing mailq to see if mail is queued. Also have you configured your mail software on the server to work?

Cheers

actually the only thing i have done as far as mail is concerned,is to install mailx on my ubuntu...:smiley:

//tried this from my command line but still no result in my inbox...

when i type mailq it says 'exim:permission denied'...

Yep. Sounds like exim configuration required.

ok i ll have to look into that cause i have noooo idea on the matter:D
thnks a lot for your help!!:wink:

I could help with exim a bit if you like....but i'm a new to this forum so not sure if its correct place seeing as this is for shell script help.

hmm i am new too...but the bottom line is that i must link mailx with the desired domain?