Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger a shell script on receiving an email

Tags:

shell

unix

How can we trigger a shell script on an unix server through an email with particular subject?

like image 686
Arc Avatar asked Feb 27 '23 12:02

Arc


2 Answers

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.

like image 166
Wim Avatar answered Apr 09 '23 00:04

Wim


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".

like image 36
dmckee --- ex-moderator kitten Avatar answered Apr 09 '23 02:04

dmckee --- ex-moderator kitten