Read a file, add some text and send an email

Hi,

If I am asking this question, you must have already figured out , that I am new to Unix, so here it goes

I was trying to read a file, add some user defined content to it and send out an email , I did find out a way to achieve this, but looking at the code, it looks a bit crude to me, can you guys point out a more efficient way of writing this?

Here is the my Input file id_list.txt

Bob
Marcus
Adam

And this is what I expect to be sent in the email

This is a test email 

This is an email to notify you that the following ID's are going to expire in 1 week 

Please change the password for "Bob" to avoid being locked out.

Please change the password for "Marcus" to avoid being locked out.

Please Change the Password for "Adam" to avoid being locked out.

And this is what I wrote

EMAIL_TEXT1="This is a test email"
EMAIL_TEXT2="This is an email to notify you that the following ID's are going to expire in 1 week"
echo 
{
echo $EMAIL_TEXT1 $EMAIL_TEXT2
{
while read userID;
 do 
 { echo -n " Please change the password for " && echo -n \"$userID\" && echo " to avoid being locked out.\n"; }
done < id_list.txt
} }> final_op.txt
mutt -s "Test Email" "karthik@xyz.com" < final_op.txt

Can someone point out a better way to write this? Also can you also explain, instead of writing the file to the final_op.txt , if i pipe it to the mutt command, the email comes up in a non formatted way, that is the text inside the while loop comes up in a single line. But if i seperate it out it comes as I have mentioned in the expected output .

Thanks,
Karthik

Try:

EMAIL_TEXT1='This is a test email'
EMAIL_TEXT2="This is an email to notify you that the following ID's are going to expire in 1 week" 
( printf "%s\n\n%s\n" "$EMAIL_TEXT1" "$EMAIL_TEXT2"
  while read userID
  do    printf "\nPlease change the password for \"%s\" to avoid being locked out.\n" "$userID"
  done < id_list.txt
) | mutt -s 'Test Email' 'karthik@xyz.com'

Could you clarify how you want the output (the mail message) not to be a single line?

Hi hanson44,
Karthi provided sample output showing exactly the format of the message wanted.

However, Karthi did not say what OS is being used. And, has been stated in this forum dozens (if not hundreds) of times before, you should never use echo if any operand contains a backslash character nor if any argument starts with a minus sign if you want to use your script on different systems. The replacement script I provided replaces the non-portable echo commands with portable printf commands and should produce mail messages in the stated format without the need for a temp file.

As far as why Karthi got a single line of output, I don't know. Even on a system that treats -n as a request to skip the trailing newline and does not translate \n to a newline, there still should have been two lines of output rather than one.