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

Hi, Here's my problem: When an email gets sent to me, I want it to call a script that will search the email for certain values. Is it possible to write a perl script that will look through my inbox for a specifically titled email, and in turn call another script to be executed? If so, how? Thanks

Replies are listed 'Best First'.
•Re: call a script via an email
by merlyn (Sage) on Jun 13, 2002 at 14:27 UTC
Re: call a script via an email
by vladb (Vicar) on Jun 13, 2002 at 14:01 UTC
    Yes, yes, it's entirely possible with Perl.

    There are two options. You could write a simple daemon (to run at all times) to monitor a POP email account. Or you could have a script run at scheduled times (via crontab) to do the same thing.

    If a certain email message is detected, you can do whatever your soul wishes, really. There's a number of modules that you could find on CPAN that will help you check a POP account from your script, parse messages, etc.

    Your script may look as simple as this (read inline comments for more info):
    use Mail::POP3Client; $pop = new Mail::POP3Client( USER => "me", PASSWORD => "mypassword", HOST => "pop3.do.main" ); my $specific_email_subject = "Parse by script"; my $parser_script = "email_parse.pl"; for ($i = 1; $i <= $pop->Count(); $i++) { foreach ( $pop->Head($i) ) { if (/^Subject:\s+($specific_email_subject)/i) { # Special email detected!!! # open a 'pipe' to the script that will parse/process # this email message body. open(PARSER,">$parser_script |"); # send email body to the script (will receive # it via STDIN print PARSER $pop->Body($i); # close pipe (end 'transmission') close(PARSER); } } }
    (note: not tested).

    UPDATE: added the sample script.

    _____________________

    signature is under repair
Re: call a script via an email
by neilwatson (Priest) on Jun 13, 2002 at 14:11 UTC
    Sounds like this would be a good job for Procmail, possibly with some perl as well depending on your requirements.

    Neil Watson
    watson-wilson.ca

      Just to expand a little on neilwatson's answer, Anonymonk, you've got the problem backwards. More likely than not, you don't want to write a perl program to poll a mailbox for new messages. You'd be much better off hooking something into your mail delivery process (procmail is the most common way to do this under *nix) which can detect relevant messages and call your perl as needed.

      One other thing: I started setting up something like what you describe once, a few years back. I quit just as it was getting to the point of usability because I realized that, with the ease of intercepting and forging email, it would be a major security risk. Consider whether your goal can be achieved without emailing commands around and, if not, be extremely cautious in your perl code. Use taint. Read all the standard precautions for securing CGI code and use them. Make sure you're using taint properly. Then go back over it and make sure you haven't missed anything.

      You could also use /etc/mail/aliases directly, similar to the way many mailing list applications do, such as Mailman, majordomo and the like. The syntax is easy:
      user@domain.com: "|/path/to/perl/script.pl" # Make sure to run 'newaliases' to regenerate

      There's also Chip Salzenberg's deliver program documented here. There are many native ways which don't rely on necessarily installing core perl modules such as Mail::Audit (tutorial here) and friends. When working on boxes where installing externally available perl modules is not an option (i.e. for paranoid sysadmins), this may be a better alternative (TMTOWTDI).

Re: call a script via an email
by frankus (Priest) on Jun 13, 2002 at 14:18 UTC
    A little more detail is needed here:
    • Platform
    • MTA

    You know all the petty stuff, we allow to distract us from the real problem ;)
    I'd use procmail and formail on a debian box :
    # Procmail recipe... lookaway. :0: * Sender:.*someone@example.com `formail -I"" |perl somescript.pl` # End of recipe.

    # Cheesy Perl code fragment. while(<>){ chomp; next unless /^command:/; my $command = $'; next if $command =~ /(:?kill -9)|(?:halt)|(?:fsck)/; system( $command ); }

    Something like that anyway.
    More importantly look at PGP verification of sender,
    we wouldn't want people spoofing emails to the address.

    Alternatively try search cpan, category mail

    --

    Brother Frankus.

    ¤

Re: call a script via an email
by elwarren (Priest) on Jun 13, 2002 at 14:30 UTC
    I used to use a completely perl based procmail replacement but I can't seem to find the name of it. I've since switched to procmail. Here are some tools for dealing with incoming mail, most probably what you want to do.
    PMS Mail::Filter Mail::Audit Sendmail::Milter
Re: call a script via an email
by boo_radley (Parson) on Jun 13, 2002 at 15:29 UTC

    if you're on a unix-y system, you might also investigate the .forward file.
    It's my only piece of advice on the subject and I'll keep dusting it off and using it as frequently as I can.

Re: call a script via an email
by Rex(Wrecks) (Curate) on Jun 13, 2002 at 16:27 UTC
    You could use Pronto as you mail client, and then edit the source to do what you want when mail comes in. That would be a fairly pure Perl solution :)

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: call a script via an email
by dmitri (Priest) on Jun 13, 2002 at 13:54 UTC
    You want "e-mail to call a script?" It is possible to write a Perl script to do almost anything. How is this question of yours Perl-specific? Maybe you should supply some info, such as your platform, mail server, your unsuccessful attempts at solving the problem, etc.
Re: call a script via an email
by greenFox (Vicar) on Jun 14, 2002 at 03:54 UTC

    Just noting that with the exception of Merlyn's solution none of the answers given here implement any kind of authentication of the sender before running the script, which means that they have a security hole which allows any-one to trigger execution the script- checking mail headers will not work as these can be spoofed.

    While it may be unlikely that someone will take the time to compromise your script or the harm minimal if they did it would be well worth the effort to at least understand Merlyn's solution. You can't spend too much time considering security.

    --
    my $chainsaw = 'Perl';