We help IT Professionals succeed at work.

email agent?

YamSeng
YamSeng asked
on
Hi,

I would like to know how can I go about to invoke a certain program upon receiving an email?

Say if I want to send an email to my unix/linux account.  Then upon receiving the email with the specified title or content, it'll invoke a program.

Please advise.
Thanks
Yam
Comment
Watch Question

Commented:
Assuming that you have an SMTP client running on your system which retrieves email on your behalf, such as sendmail or fetchmail, you can use either builtin commands (sendmail uses ~/.forward) or one of the favorite procmail to setup filter rules and even execute commands upon the header or content of an email. Or you can write your own.

Typically, the process is as follows:

using a .forward file, you specify a command to pipe your incoming email to "|/usr/local/bin/procmail -T". Procmail then will read your ~/.procmailrc file to see what rules you set. Depending on a regular expression, a rule may be activated or the mail gets sent to the defined default mbox.


K.

Author

Commented:
Unfortunately, my System administrator has disabled the .forward file in the mail server for some other reasons.  Is there any other way to go about it?

I'm using pine to access my mails in my Linux box.

Correct me if I'm wrong in the interpretation of your comment.  You are saying that I can use .forward invoke procmail to process the emails to parse the details of the email to determine the 'right' invoking email.

So can you give me some examples of how I can use procmail?  ie, the email I want to capture and then to activate a certain program has the following details..
1) from email address: yamseng@somewhere.com
2) has subject: invoke program
3) has content: content123

Please advise
regards
Yam

Commented:
Procmail works quite neat. It takes your email on stdin and parses it for header fields and (if needed) the message body too. Depending on the configuration, it will involve some actions from executing program to forwarding..

As far as the execution of scripts goes, the following will probably do what you want. Please be very very careful in how you use this. Unfortunately, email headers can be sooo easily manipulated that anyone who knows your script will be able to use your script and run it on your account.


-=-=-=-
PATH=/usr/local/sbin:/usr/bin:/bin:/usr/sbin
MAILDIR=$HOME/Mail
DEFAULT=/home/$USER/mbox
LOGFILE=$MAILDIR/procmail.log

:0
* ^From:.*yamseng@somewhere.com
* ^Subject: invoke program
| $HOME/myexec

-=-=-=-

And create a executable $HOME/myexec :
#!/bin/sh
while read line; do
    sh -c $line;
done

-=-=-=-

As far as the .forward is concerned: you could run a cron job which pumps your /var/log/mail file through procmail in regular intervals, e.g. ever 10 mins. And you can use formail within the .procmailrc to detect dublicate mails by putting the following statement at the
beginning of your file:

# Prevent duplicate (crossposted) messages by using an 8Kb
# message-id cache. Succeeds if so and stops any further
# processing
:0 Wh: msgid.lock
| formail -D 65536 msgid.cache

:0
* ^From:.*yamseng@somewhere.com
* ^Subject: invoke program
| $HOME/myexec

-=-=-=-=-

Commented:
Correction for procmailrc in order to filter out the header stuff (by searching for an empty line).

| awk '/^$/{start=1};{if(start){print}}' | myexec

I haven't had a chance yet to test this is full. So be careful when implementing this. But it should work as expected.

K.

Author

Commented:
hmm?  where do I put the new corrected line in?
Commented:
Here is the corrected version:


# Prevent duplicate (crossposted) messages by using an 8Kb
# message-id cache. Succeeds if so and stops any further
# processing
:0 Wh: msgid.lock
| formail -D 65536 msgid.cache

:0
* ^From:.*yamseng@somewhere.com
* ^Subject: invoke program
| awk '/^$/{start=1};{if(start){print}}' | myexec

Any progress ?

K.

Author

Commented:
I'ven't tested it yet.  I've to wait for like 1 month from now before I can get my hands to it as I'm working on other things now.

Nonetheless I'll grade you now.  When I've got problems, I'll post a new question.

Cheers!
kian

Commented:
I don't think you have graded it yet. This thread is still open

Explore More ContentExplore courses, solutions, and other research materials related to this topic.