Run shell script when receiving email

How can we run a shell script on a unix server via email with a specific question?

+4
source share
3 answers

procmail allows you to act on incoming messages, including filtering and running external commands.

Some useful links:

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

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

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

: 0 * conditions, if any | OWN script here

Conditions in their simplest form are regular expressions to match the header of each incoming mail message. Correction: Even easier, you can completely exclude condition lines if you want to do your action (in this case, run the shell script) unconditionally.

More difficult conditions can also be exit codes from another shell scripts or programs or tests against the entire message body, or against Procmail variables (Procmail variables are also exported to the subprocess environment, therefore they are essentially environment variables. This is described in detail in this FAQ. )

Actions can also be to save the message to a folder (added to the Unix mailbox file or written to a new file in a directory) or forward the message to one or more other addresses. Finally, an action can be a nested block of more “recipes,” as these conditional comparisons are called in the Procmail jargon to try if the external state is fulfilled. There is a complete scoop on the procmailrc (5) manual page.

Obviously, you are not limited to Perl or shell scripts. You can start something from a Unix command line that can be launched from Procmail, in principle, although running interactive programs usually does not make much sense.

+6
source

More general, but in my opinion less useful than the wim procmail clause : you can even simply specify your .forward in the executable using "| scrip.sh" .

+2
source

You could theoretically write a monitoring / polling program for the incoming mail server and check the subject line using the standard POP3 protocol, if there are certain trigger keywords in the subject line, call the shell script ... This is the order of the approach that would work ... there maybe an open source solution ...

  • Using sockets, connect to the incoming mail server by IP and port (usually 25), non-blocking, so as not to capture and digest processor time, during the cycle cycle forever
  • List emails using the POP3 protocol
  • Pull the headers through the POP3 protocol and regex in the subject line
  • If the regex matches the subject line, enter a trigger, possibly a system call to invoke a shell script
0
source

All Articles