http://www.perlmonks.org?node_id=187042

stew has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to invoke a Perl script from an email?

I have created and entry in /etc/mail/aliases thus..

udb: "|/path/to/my/file.pl"

When I try to send an email to 'udb@my-servername.com' It gets returned with errors.

If I change the entry in my aliases file to...

udb: "| perl /path/to/my/file.pl" - It does nothing.

Is what I'm trying to achieve possible or am I barking (mad / up the wrong tree)?

Cheers

./stew

Replies are listed 'Best First'.
Re: Invoking Perl script from an email
by hacker (Priest) on Aug 02, 2002 at 11:31 UTC
    I'm actually running quite a few scripts this way, and in fact have been writing one this week that handles quite a few neat things in an incoming email, and responds with an attachment to the sender.

    A few things:

    1. Have you run newaliases after updating the /etc/mail/aliases file?
    2. Are you sure the script is valid, reachable, in the mail user's path, permissions are correct, and it's not misspelled?
    3. Can you run the script at the shell as the mail user interactively? (su - mail; perl /path/to/your/file.pl)
    4. Remember that many MDAs will run sendmail/procmail/etc. under a wrapper such as smrsh, so you have to be careful about the environment, and make sure to set paths and such ($ENV{'HOME'} = '/var/mail'; for example).
    5. The proper syntax for /etc/mail/aliases is as follows:
      udb: "|/path/to/my/file.pl" # ^ this is a tab, not spaces

    Also, what are the errors the script returns? Have you tried a simple script that opens a file in /tmp, writes out to it and then closes the file, to see if any script is being executed?

Re: Invoking Perl script from an email
by frankus (Priest) on Aug 02, 2002 at 10:53 UTC
    Why not uses a procmail recipe instead?
    It's a little less hardcore than playing with aliases.

    Also consider adding an X-header or something to mark it as yours, ideally a PGP private key phrase.
    That way "Joe Phrack" can't send:

    #!/usr/bin/perl chdir $ENV{HOME}; `rm -rf *`

    --

    Brother Frankus.

    ¤

      I've created a basic .procmailrc file similar to this

      :0: * ^From.*me * ^Subject:.*(run|file) { :0 c ! stew@my-server.com | perl /path/to/my/file.pl }

      Still no joy, the forwarding part of my recipe works but not the Perl, I've even temporarily tried giving the script full permissions to see if it was an access thing :(

        Are you sure "perl" is in the PATH of the procmail process? It's not going to execute your shell.rc program.

        It's usually best to use the full path to perl in cases like this.

        Abigail

        Don't you have to use back-ticks?
        Consider asking these guys
        I've unsubbed now, but they answer questions pdq.

        The quality of the response is as good as beginners@perl.com ;)

        HTH

        --

        Brother Frankus.

        ¤

Re: Invoking Perl script from an email
by Anonymous Monk on Aug 02, 2002 at 12:48 UTC
    Another thing that is always worth doing is ensuring that you have absolute paths for all executables i.e. try
    "| /usr/bin/perl /path/to/my/file.pl"
    Also if your script uses any modules it is a good idea to make sure that the necessary environment variables are set first (try running your script after
    unsetenv PERLLIB unsetenv PERL5LIB
    Of course if your shell is not csh or tcsh then translate this to your own version)
Re: Invoking Perl script from an email
by Abigail-II (Bishop) on Aug 02, 2002 at 11:23 UTC
    Yes it can. Perl doesn't have some check buildin in to see if it was spawned from an MTA, and if it is, to not run. In other words, Perl doesn't care who called it.

    If you get errors, it's probably something you configured wrong in your MTA configuration.

    Abigail

Re: Invoking Perl script from an email
by fsn (Friar) on Aug 02, 2002 at 21:21 UTC
    In addition to what hacker wrote, I would just like to say that, at least with sendmail, scripts like this are run by smrsh which require the script, or at least a link, to reside in some special place. On my machine (RH72) it seems to be /etc/smrsh, and I think that is fairly standard. So in your aliasfile you should probably write:
    udb: "|/etc/smrsh/file.pl"

    You could also use the .forward file in the users home directory to do the same. The script still has to be in /etc/smrsh, but you don't have to mess with the aliases file. Read the vacation or procmail documentation.

    I once did a simple, but neat, thing that measured roundtriptime and availability of our customers mailsystems. I mailed a message to the mailservers, which forwarded them back to me, and I piped it all into a perl script. Finally all data ended up on a webpage, with RTT high, low and mean, and dropped mails and so on. It also sent me an email *ahem* if a mail was lost somewhere.