Operating System - HP-UX
1829102 Members
2594 Online
109986 Solutions
New Discussion

How to set rules for mailx?

 
SOLVED
Go to solution
Sanjay Jadhav (Pune)
Occasional Contributor

How to set rules for mailx?

Hi!
I am pretty beginner... Is it possible to set rules on mails/mail? similar to outlook (at least simple rules?).

Example I want filter out and save those message automatically if sent by perticular sender.

Thanks,
Sanjay
3 REPLIES 3
Matti_Kurkela
Honored Contributor
Solution

Re: How to set rules for mailx?

In unix-style email environment, this can be done in a different way.

Instead of doing the filtering in the client (or Mail User Agent), you can make the mail server (Mail Transfer Agent; sendmail in HP-UX) do the filtering. The classic tool for this is called "procmail": it is available from the HP-UX Porting Archive.
http://hpux.cs.utah.edu/hppd/hpux/Networking/Mail/procmail-3.22/

Here's an introduction to the use of procmail:
http://userpages.umbc.edu/~ian/procmail.html

With procmail, you can create rules using regular expressions, matching either mail headers, mail content or both. You can also make procmail feed your email to any program or shell script you wish, and make filtering decisions according to the result code of those programs. You can even make those programs pre-process the content of your emails, if you wish.

Note that there are several ways to install procmail: you can just install the binary and use the $HOME/.forward file to enable the use of procmail on a per-user basis. Or you can make slight modifications to sendmail configuration to make procmail be the "local delivery agent", so all users can use procmail by simply writing the rules they want to their $HOME/.procmailrc. If the $HOME/.procmailrc does not exist for some users, procmail works just like sendmail's standard local delivery agent.
MK
Sanjay Jadhav (Pune)
Occasional Contributor

Re: How to set rules for mailx?

Thanks Matti!
I wasnt interested in mail server, actually I am able to read mails using mailx/mail/pine (mostly I use mailx). Again newly heard of procmail from you... I do not see .procmailrc Can I create a new file and edit it? I will appreciate if you point out some examples (if time permits). Actually I am interested in saving mail attachments (not all... only for maill sent by perticular user which also have same subject pattern). Is it possible to save attachment to some folder automatically by setting rule?

Thanks in advance.

Sanjay
Matti_Kurkela
Honored Contributor

Re: How to set rules for mailx?

Pine might have just enough filtering functionality to do what you need, but mail and mailx definitely do not have any filtering rule functionality, so you might need a procmail-based solution.

If you install and use procmail, you must write your own .procmailrc. If none exists, procmail does not change the default email behavior at all.

If you want to automate the handling of attachments, you'll need the "munpack" tool. It's available in the "mpack" package in the Porting Archive:
http://hpux.cs.utah.edu/hppd/hpux/Users/mpack-1.6/

The necessary .procmailrc rule for your case would be something like this:

:0 W: attachment-extractor.lock
* ^From:.*someaddress@somewhere\.example
* ^Subject: subject-pattern
| munpack -C /desired/attachment/directory

The first line sets up a lockfile "attachment-extractor.lock", so that if the mail server happens to handle several matching mails in parallel, their handling is serialized here to prevent filename conflicts. You can choose the name of the lockfile freely: procmail will create the lockfile when starting to process an email that matches this rule, and delete the lockfile when processing is complete. If the lockfile already exists (i.e. another matching email is currently being processed), procmail waits.

The next two lines define the match conditions. These are standard regular expressions: note that a single dot means "any single character can be at this location", so if you want to specify an explicit dot (like in an email address), you should put a backslash before the dot. The combination ".*" means "here can be any number of any characters". See "man 5 regexp" for a full description of regular expression syntax.

The final line tells what must be done if a mail matches all the conditions. In this case, it will be piped to "munpack", which will extract any attachments from it. You'll need to specify the directory where you want to store the attachments (the -C option). By default, munpack will not overwrite any old files: if there is an existing file with the same name as the new attachment, munpack will add ".1", ".2" etc. to the end of the new filename to avoid name conflicts. If you need to overwrite the old attachments, munpack can do that too: see its documentation for details.
MK