简体   繁体   中英

Can we trigger a perl script in unix by sending a mail

I want to know if we can trigger a perl script in unix through a sending a mail. basically the script should check the incoming mail then trigger the perl script.

Also can some1 out on setting the mail access like reading and saving mail on my unix home.

-Thanks

There are two basic approaches you can take.

  1. Configure your SMTP server to run incoming email through your script. Procmail is the usual tool for choice for this.
  2. Poll (by using cron, or writing your script as a daemon) your IMAP/POP server/Maildir/Mbox/etc.

The former is usually the better option.

You can hack it like this:

inotifywait -m  /var/mail/$USER | grep --line-buffered MODIFY | while read _unused_; do 
    #your perl script here
done

Explanation: It monitors /var/mail/$USER for changes & prints events on stdout. On every MODIFY event, it will trigger the script.

Note: This will work only for unix mails on localhost. Not on external server.

Known bugs: A mail read activity will also trigger the script.

You can keep polling (unix) mail for any new mails.

while true; do
    echo "\nq" | mail >/tmp/new_mail 2>&1 && /path/to/your/perl_script.pl arg1 arg2 ...
    sleep 60
done

If you want, you can use contents of /tmp/new_mail in your perl program. If you don't need it, you can redirect mail output to /dev/null instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM