Automating Mail Process

Hi
i had written one script,it sends email from terminal and mine script is as:

#!/bin/bash
SUBJECT="linux mail send attachment example"
BODY_FILE="/home/sreenivasa/Desktop/Testing.txt"
#ATTACHMENT_FILE="/home/sreenivasa/Desktop/Dataset.zip"
CC_LIST="rajnikant875@gmail.com,sinha6315@gmail.com,rajnikant@gytechs.com"

cat "$BODY_FILE" | mail -s "$SUBJECT" -a "$ATTACHMENT_FILE" "$CC_LIST"

but here problem for me is i can attach only one attachment.But my requirement is all attachment file is in one folder without changing script again and again,how can i access those files from folder which is saved somewhere on Desktop.

So,we have to write script once and when we ran script it should ask for which all file it want to attach into script.Can anyone help me.

Use for-loop. But I suppose you would want to change the email body and subject per attachment. I leave it to you to figure it out.

for ATTACHMENT_FILE in /path/to/folder/* # assuming folder has only files that are to be attached. Tweak accordingly.
do
    SUBJECT="linux mail send attachment example"
    BODY_FILE="/home/sreenivasa/Desktop/Testing.txt"
    CC_LIST="rajnikant875@gmail.com,sinha6315@gmail.com,rajnikant@gytechs.com"
    cat "$BODY_FILE" | mail -s "$SUBJECT" -a "$ATTACHMENT_FILE" "$CC_LIST"
done
1 Like

A minor tip - remove the useless use of cat

change

cat "$BODY_FILE" | mail -s "$SUBJECT" -a "$ATTACHMENT_FILE" "$CC_LIST"

to

mail -s "$SUBJECT" -a "$ATTACHMENT_FILE" "$CC_LIST" < $BODY_FILE
1 Like

What have you tried so far for research? This is a fairly common question with lots of threads in various forums.

This might help:- Unix multiple attachment

I hope that this helps. Let us know how you get on and if you are still stuck.

Robin

1 Like

Hi
my script can send mail and attachment without any issue.The problem i am facing is that:there are folder assume at Desktop which has multiple files e.g. pdf,zip,txt,jpg...e.t.c.So, How my script should incorporate all these files automatically.
Here i am thinking assigning all these attachment to one variable name and just by calling that variable in script should work.Good or bad,please share your thoughts.Thank u.

Hi rajnikant,

this answer may help you a lot: Automating Mail Process Post: 302978324

regards,
stomp

2 Likes

Are you saying that you need help to attach all the files from the a directory, number unknown?

If so, then you will need a loop inside the section of you script that sends the note.

Before we get to that, can you send 2 or 3 attachments in a single note from a script yet? It's important to understand how you feel comfortable doing it before we proceed else you might end up with something you don't understand which is a nightmare to support or adjust if needed.

Paste your code in CODE tags so it is clear to see.

Kind regards,
Robin

1 Like

Hi rbatte1
U r correct.i should be more specific.at present this script can send max 2 attachment at a time.but issue here is we can not modify the script all time.once we give it to our boss,he can not changes again and again.Before submitting the script we should make it proper. So script should automatically ask user these many files are available under this particular Folder and which all file you want to attach them with mail.e.g.

  1. 1st list all the attachment with serial#.
  2. Then user should enter i want from 2-5 and 6-8.
  3. Finally these name should be attached in script

If you want to magically guess at which files in your Desktop directory are intended to be attachments, it won't be easy (unless you can explain and implement the magic). If you are willing to move all of the files that you want to be treated as attachments into a directory in your Desktop directory (in the following example, that directory is named attachments ), you might want to try something like:

#!/bin/bash
ATTACHMENT_DIR="/home/sreenivasa/Desktop/attachments"
ATTACHMENTS=
BODY_FILE="/home/sreenivasa/Desktop/Testing.txt"
SUBJECT="linux mail send attachment example"
TO_LIST="rajnikant875@gmail.com,sinha6315@gmail.com,rajnikant@gytechs.com"

# Gather list of attachments:
for file in "$ATTACHMENT_DIR"/*
do	[ ! -f "$file" ] && continue
	ATTACHMENTS="$ATTACHMENTS -a \"$file\""
done

# Send the message.
mail -s "$SUBJECT" "$ATTACHMENTS" "$TO_LIST" < "$BODY_FILE

Note that the text marked in red above serves two purposes:

  1. it only selects regular files from that directory to be used as attachments, and
  2. it keeps an error from occurring in the mail command if that directory doesn't exist or is an empty directory.

Note also that I renamed the variable CC_LIST to TO_LIST because the e-mail addresses in that list will be on the To: list in the mail message that is sent; not on that message's Cc: list.

2 Likes

Thanks Don Cragun for quick reply.
i am trying to run this script but it is unable to attach any kind of files from that particular folder.it is sending mail but without any attachment.
My requirement is like, script should attach all files from that particular folder.
One thing i did not understand what is use of variable ATTACHMENTS and file.

Can u please brief me.

Thank u
Rajnikant

In the manual page for mail on your server, do you have the -a flag? (not the same as the -A flag)

If not, then this will not work.

Robin

Hi Robin,
Note that Rajnikant said that the script in post #1 in this thread was working to send mail with a single attachment (using the mail -a option). I don't know of any versions of mail that accepts a -a option that won't accept multiple -a options.

Hi Rajnikant,
The for loop in my script executes the commands inside the loop once for each file in the directory /home/sreenivasa/Desktop/attachments . Each time it executes the commands in that loop, the shell variable file will be set to the name of one of the files in that directory. For every regular file it finds in that folder it adds -a and the name of that file (in quotes) to the shell variable ATTACHMENTS which is then used in the mail command to include those files as attachments to the message it sends.

To avoid sending unwanted mail while we are testing, please change:

mail

on the last line of your script to:

echo mail

Then show us the output you get from running the commands:

ls -l /home/sreenivasa/Desktop/attachments
     and
od -bc < your_script_name
     and
bash -xv your_script_name

where your_script_name is the name of the file containing your script.

1 Like

Hi Don Cragun
u asked me to run the script and few commands.i ran it and attached it with this message.please have a look at Attach Files.

Thank U
Rajnikant

Hi Rajnikant,
In the future, when we ask for the output you get from running a command; please show us the output as text as it appears on your screen (presumably in a fixed width font as provided by CODE tags in the body of the post), not a Microsoft Word document given as an attachment using a variable width font and adding unwanted page breaks that hides much of the clarity in the output we need to see to help you.

I suggested that you should have:

mail -s "$SUBJECT" "$ATTACHMENTS" "$TO_LIST" < "$BODY_FILE"

as the last line of your script. Instead of that line, you used:

mail -s "$SUBJECT" "$ATTACHMENTS_DIR" "$TO_LIST" < "$BODY_FILE"

The trace from running your script clearly shows that it correctly set the variable ATTACHMENTS to include eight attachments, but you didn't expand that list in your invocation of mail . Instead you expanded the variable ATTACMENTS_DIR (which is never defined by your script and, therefore, expands to an empty string throwing away all of the work the script did to get your desired attachments).

1 Like

Hi Don Cragun

  1. i ran the script with echo keyboard inside the script as u instructed and it is unable to send the mail,it just displays the files inside that Folder.
/sreenivasa@UIPL08:~/Desktop$ ./Experiment1.sh
#mail -s linux mail send attachment example  -a "/home/sreenivasa/Desktop/ATTACH/Arduino.doc" -a "/home/sreenivasa/Desktop/ATTACH/Bug.jpg" -a "/home/sreenivasa/Desktop/ATTACH/Bugzilla.pdf" -a "/home/sreenivasa/Desktop/ATTACH/Dataset.zip" -a "/home/sreenivasa/Desktop/ATTACH/diet.jpg" -a "/home/sreenivasa/Desktop/ATTACH/Raj.txt" -a "/home/sreenivasa/Desktop/ATTACH/RFQ.xlsx"  rajnikant875@gmail.com,sinha6315@gmail.com,rajnikant@gytechs.com
  1. And when i use the same script with single attachment it will send mail with that particular attachment.

  2. When i ran same script without echo keyword,it displays output something as:

sreenivasa@UIPL08:~/Desktop$ ./Experiment1.sh
 -a "/home/sreenivasa/Desktop/ATTACH/Arduino.doc" -a "/home/sreenivasa/Desktop/ATTACH/Bug.jpg" -a "/home/sreenivasa/Desktop/ATTACH/Bugzilla.pdf" -a "/home/sreenivasa/Desktop/ATTACH/Dataset.zip" -a "/home/sreenivasa/Desktop/ATTACH/diet.jpg" -a "/home/sreenivasa/Desktop/ATTACH/Raj.txt" -a "/home/sreenivasa/Desktop/ATTACH/RFQ.xlsx": No such file or directory
  1. i tried using mailx,uuencode but none of them are working with multiple attachment.But for loop is able to attach all the files from ATTACHMENTS folder.

  2. Please share your input.

Thank U
Rajnikant

---------- Post updated at 10:38 AM ---------- Previous update was at 10:36 AM ----------

The code which i am using is:

/#!/bin/bash
ATTACHMENT_DIR="/home/sreenivasa/Desktop/ATTACH"
ATTACHMENTS=
BODY_FILE="/home/sreenivasa/Desktop/Testing.txt"
SUBJECT="linux mail send attachment example"
TO_LIST="rajnikant875@gmail.com,sinha6315@gmail.com,rajnikant@gytechs.com"

# Gather list of attachments:
for file in "$ATTACHMENT_DIR"/*
do    [ ! -f "$file" ] && continue
    ATTACHMENTS="$ATTACHMENTS -a \"$file\""
done

# Send the message.
mail -s "$SUBJECT"  "$ATTACHMENTS" "$TO_LIST" < "$BODY_FILE"

Sorry. My mistake. There should not be double quotes around the expansion of $ATTACHMENTS in the mail command. Please try this:

mail -s "$SUBJECT"  $ATTACHMENTS "$TO_LIST" < "$BODY_FILE"

instead of:

mail -s "$SUBJECT"  "$ATTACHMENTS" "$TO_LIST" < "$BODY_FILE"

Hi Don Cragun
Case1: i used the

mail -s "$SUBJECT"  $ATTACHMENTS "$TO_LIST" < "$BODY_FILE"

Output i am getting is like this on console:

sreenivasa@UIPL08:~/Desktop$ sudo ./Experiment1.sh
"/home/sreenivasa/Desktop/ATTACH/Arduino.doc": No such file or directory

Case2: When i use

mail -s "$SUBJECT"  "$ATTACHMENTS" "$TO_LIST" < "$BODY_FILE"

Then it would send mail without any attachment,i had attached one snapshot of that.please have a look.

Thanks
Rajnikant

Hi rabatte
Script is able to send mail with single attachment but when it comes to multiple attachments.it is unable to do so.Have a look and share your thoughts.

Code which i am using is code:

#!/bin/bash ATTACHMENT_DIR="/home/sreenivasa/Desktop/ATTACH"  ATTACHMENTS= BODY_FILE="/home/sreenivasa/Desktop/Testing.txt"  SUBJECT="linux mail send attachment example"  TO_LIST="rajnikant875@gmail.com,sinha6315@gmail.com,rajnikant@gytechs.com"   # Gather list of attachments: for file in "$ATTACHMENT_DIR"/* do    [ !  -f "$file" ] && continue     ATTACHMENTS="$ATTACHMENTS -a  \"$file\"" done  # Send the message. mail -s "$SUBJECT"  $ATTACHMENTS  "$TO_LIST" < "$BODY_FILE"

Thanks
Rajnikant

---------- Post updated at 07:44 AM ---------- Previous update was at 07:14 AM ----------

Hi balajesuri
for loop is working but it is sending all the attachment one by one.Can we modify it in a way that it send all the attachment in a single mail.please share your thought.

Thanks
Rajnikant

---------- Post updated at 07:50 AM ---------- Previous update was at 07:44 AM ----------

Hi Don Cragun
Here is another piece of code but problem here is, it send all attachments one by one, not all at time.

#!/bin/bash
for ATTACHMENT_FILE in /home/sreenivasa/Desktop/ATTACHMENTS/* 
do
    SUBJECT="linux mail send attachment example"
    BODY_FILE="/home/sreenivasa/Desktop/Testing.txt"
    CC_LIST="rajnikant875@gmail.com,sinha6315@gmail.com,rajnikant@gytechs.com"
    mail -s "$SUBJECT" -a $ATTACHMENT_FILE "$CC_LIST" < "$BODY_FILE" 
done

Can u share your idea to make it better.
Thanks
Rajnikant

OK. I understand what I did wrong, but my problem is that when you have filenames like RFQ - Medical - 15-05- 2016.xlsx (containing whitespace characters) we have to quote the filename to keep it from being treated as six separate files, but gathering a group of quoted strings in a variable and expanding that variable ends up with the quotes being part of the filename.

Can you rename your attachment files so that they do not include any spaces or tabs before you call this script?

If not, do you mind if the script renames the files in the attachments directory (and leaves them with new names not containing spaces or tabs)? (Note that if any of the directories in the pathname of you attachments directory contain any whitespace characters, I am not offering to make a script for you that will work in those circumstances!)

1 Like