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!

Automating mail content to file by mail subject 1

Status
Not open for further replies.

UnixClueless

Technical User
Sep 13, 2001
10
AU
Hi All,

Is there an easy way of searching through standard unix mail for a subject and then saving the content of the mail message to a file?

Any ideas would be greatly appreciated.

Cheers
 
hi there,

You can do this in a couple of ways:

1 - Easy - As the mail arrives you can pipe it through a shell script and then into a file if it matches the subject you're looking for.

2 - Bit harder - write a script that reads through your mailbox file searching for email with the right subject, the write just that email to a file.

Which was would you like to try?

What UNIX platform are you using? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Most semi-sophistocated mail tools can make Mike's option 1 even easier. You set up flags to handle (save/forward/delete/etc.) messages based on different criteria (subject/sender/etc.).

My personal favorite is elm (because I could run it on all the *ix systems I've used over the years - I'm very familiar with it!), but there are a number of others with that capability as well.

- Steve StevieW85@go.com
 
Hi Mike,

Your first suggestion was to pipe the mail through a shell script as it arrives. How would you go about doing this? Is there any documentation you could point me to?

Thanks Ange
 
Ange,

Easiest way to try this is to create a file in your home folder call .forward and put one line in there

|tee /tmp/tmpmail.txt

This will pipe *all* mail you get through this command, and each email will end up in the file /tmp/tmpmail.txt - it will also get overwritten each time.

That's the first step, to actually do something with mail as it arrives. Next is to do something with just the mail you're interested in.

Create this shell script in your home directory and name it .process_mail.ksh

#!/bin/ksh

export INTERESTING_SUBJECT='Subject: TCP' # well, it is!

cat > /tmp/mail.$$.txt

if [[ egrep $INTERESTING_SUBJECT /tmp/mail.$$.txt > /dev/null 2>&1 ]]
then
# it's an interesting email, copy whole thing into a file.
cp /tmp/mail.$$.txt /tmp/mail.interesting.$$
fi

do a

chmod +x .process_mail.ksh

and then change the line in .forward to read:

|.process_mail.ksh
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Thanks Mike,

I will have a play around with that today and let you know if I have any issues.

Steve,

Thanks for your suggestion too, I'm not sure how or where you set up the flags you indicated, but I'd be interested in finding out some more. I want to try a number of solutions to see which one would suit what we are doing best.
I don't think elm has been set up on our unix servers, I have tried to use but it is not available. If need be I will get our Unix administrators to set it up.

Thanks Ange
 
I use elm primarily because I got tired of having to learn a new GUI based mail tool each time I switched systems. Elm was constant. (Pine is quite similar.)

The mail tool installed on your system probably has all the necessary capability. Rather than recommending you use elm, I suggest you use what's already available. If you decide to use elm, you can set things up through elm or edit the .elm/filter_rules file directly.

Look for something containing "Filter" (e.g., "Filters" or "Message Filters") under the File tab or Edit tab of your GUI. Different tools use different methods, but they are typically pretty easy to set up. If you have specific questions, feel free to ask, and I'll try to help.

Mike's .forward method offers you the benefit of much more control as to what you do, but at the possible expense of more work on your part.

- Steve StevieW85@go.com
 

Steve,
Thanks for your reply. At this stage I will go with what we have, but I will keep the elm in mind just in case.

Mike,
I have been playing around with the .forward, but at this stage I have hit a brick wall. Currently we do not have the /etc/shell set up at our clients' site so I am unable to successfully test this option.
It will probably take me some time to get this implemented, because the unix administration is outsourced and getting approval for even the smallest things, can sometimes be like climbing Mt Everest.

I'm sure that this option will work for what we need to do.
Will let you know how I get on sometime in the future.

Cheers Ange


Thanks again to both of you for all the advice,it has been invaluable. Keep up the good work.
 
Hi Guys,

While we're on the subject of mail, any ideas about automating the following:
searching unix mail by subject and/or sender and date
and then deleting it.

Should I start a new forum?

Cheers Ang
 
This thread is OK by me, but it's up to you. :) (If you really want to go deeper, it would probably be a good idea to start a new thread.)

Your mail tool is probably best here. You can do something like a select on the whatever criteria, then delete the selected messages. Otherwise...

You probably need to do something like Mike's suggestion 2 above. It gets a little trickier, though, because you have to update the mail file. The easy way to do that is to write a new file with the unwanted messages deleted, then move the new file to the old file. Problem is, what if mail happens to come in between when you read your mail file to start processing and when you copy over the edited file? You lose that mail.

How do you handle that? I used to play with that sort of thing a lot, but that was probably almost (gulp) 10 years ago! If you decide to go this route, post again, and I'll try and shake off the cobwebs.

The trick is to use your input filter to eliminate these messages when they arrive. (I'm assuming that's what you're trying to do.)

Hope this helps!

- Steve StevieW85@go.com
 
"How do you handle that?"

If you flock() the mailbox file it will work, unix mail delivery agents honour file locks.

The problem I've had processing mailbox files is that, unless I can use a language like perl, it's quite difficult to spot when the next email starts. :) Probably because I'm such a Perl freak Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Hi Guys,

Thanks again for taking the time to offer your expertise on this subject. I have now been asked to look further into deleting unix mail by subject,sender and date so I am back in the hope that you can offer me more help.

Steve, you mentioned that the mail tool was the best option. Is this a manual solution or can this be automated?

I don't mind setting up a perl or shell script solution, if that's what it takes. Do either of you have any sample perl or shell scripts that may do something of a similar nature that I might be able to use as a starting point.

Greatly appreciate your help.
Ang
 
Ang,

Sorry for the delay in answering; couple of questions:

Did you get past the /etc/shell hurdle?

Do you need to process mail as it arrives or do you need to process mail that has already arrived?

What mail client(s) do you have installed? (elm etc) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Ang,

When I suggested that your mail tool is best, I was talking about deleting mail that is already in your mailbox. Use the sort capabilities to select unwanted mail - perhaps moving suspect mail to a new folder and using a different sort criteria to narrow the selection - then delete it. This would be a manual process.

For mail that is coming in, the mail tool filters might be what you want. A filter script would also work. Which choice is best depends on the complexity of your deletion criteria and how much you want to/can put into it. Either way, this would be an automated process (once you set up your filters).

Mike's second question (at what stage do you want to process mail) is quite pertinent.

Perhaps more detail on what you are trying to accomplish is in order. Unless you plan on processing several mail boxes, I don't see much way of automating the process. Well, maybe I do… if you don't use the automated filter to process incoming mail, you could automate the process of cleaning up afterward. The best solution here would be to set up a filter on the incoming mail, however, and not play with the mailbox. (I'm assuming that once you clean up your mailbox, you will use filters to keep it clean.)

To offer much more, I need a better idea of what you are really trying to accomplish.

- Steve StevieW85@go.com
 
Thanks Guys,

At this stage we haven't got past the /etc/shell hurdle, but will do when required.
What we actually want to do is process the mail after mail has arrived. We want to be able to run some sort of script to find any particular piece of mail by passing the subject, date and sender and then delete it. I don't think the filters are going to be suitable.
I'm not sure what mail clients we have, I use the mail command to check mail at the moment. I know we certainly haven't go elm as I have tried to run it. But if you know of any that will make this process of deleting easier, please let me know.

Hope I have given you enough information.

Cheers Ang
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top