开发者

Trigger a shell script on receiving an email

How can we trigger a shell s开发者_如何学编程cript on an unix server through an email with particular subject?


procmail allows you to act on incoming mails, including filtering and starting external commands.

Some useful links:

  • general procmail documentation: http://pm-doc.sourceforge.net/doc/
  • start a shell command as a procmail rule: http://porkmail.org/era/procmail/mini-faq.html#rtfm

Just in case the link goes down, this is the link from the second point from above:

Q: How can I run an arbitrary Perl or shell script on all or selected incoming mail?

A: Install Procmail. Read the manual pages (there are several). Thank you.

:0 * conditions, if any | your-script-here

The conditions, in their simplest form, are regular expressions to match against the header of each incoming mail message. Correction: Even simpler, you can leave out the condition lines completely if you want to do your action (in this case, run a shell script) unconditionally.

More-complicated conditions can also be exit codes of other shell scripts or programs, or tests against the full body of the message, or against Procmail variables (Procmail's variables are also exported to the environment of subprocesses, so they are essentially environment variables. There are details about this later in this FAQ.)

Actions can also be to save the message to a folder (appended to a Unix mailbox file, or written to a new file in a directory) or to forward the message to one or more other addresses. Finally, the action can be a nested block of more "recipes," as these condition-action mappings are called in Procmail jargon, to try if the outer condition is met. The procmailrc(5) manual page has the full scoop.

Obviously, you are not restricted to Perl or shell scripts. Anything you can run from a Unix command prompt can be run from Procmail, in principle, although running interactive programs doesn't usually make much sense.


More general, but to my mind less useful than Wim's procmail suggestion: You can even just point your .forward at an executable with "|scrip.sh".


You could in theory, by writing a program to monitor/poll the incoming email server and check the subject line using standard POP3 protocol, if the subject line has a particular trigger keywords, invoke the shell script... This is the order of approach that would suit... there may be an open source solution already out there...

  • Using sockets, connect to the incoming email server by IP and port (usually 25), non-blocking that is not to seize up and chew up CPU time, within a thread looping forever
  • List the emails using the POP3 protocol
  • Pull down the headers via POP3 protocol and do a regexp on the subject line
  • If the regexp matches the subject line, issue a trigger perhaps a system call to invoke the shell script
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜