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

Goal: Read newest message in gmail, look for keywords in message body, delete message. Repeat on a cron job every 10 minutes.

I'm thinking IMAP, hence my earlier issue with a dependency of Net::IMAP::Simple. But is there something better, or something already designed for this?

We send out email confirmations at work, and a least a few times a day we get "undeliverable" ones that come back to us. Usually typos in the email address.

I've filtered all of these auto-replies from postmaster into a separate gmail account with IMAP access enabled.

Now, we need to detect our internal confirmation numbers in the message bodies so that we can have our perl scripts automatically alert staff of the customer name (having looked up the confirmation number) who needs to be contacted for updated information.

The ONLY problem I'm having is grabbing the messages from the gmail server

  • Comment on Most elegant way to process an email in a gmail account?

Replies are listed 'Best First'.
Re: Most elegant way to process an email in a gmail account?
by JavaFan (Canon) on Feb 06, 2012 at 12:23 UTC
    Forward your mail to a shell account that runs qmail.
    $ echo '| /path/to/my/script'
    Now your script gets the email on stdin. Do with it what you want using your favourite language. And qmail is just an example; any decent MTA has this ability.

    And you send your *work* mail, with *customer data* to Google? You don't mind the biggest data mining coorporation getting access to your email content?

Re: Most elegant way to process an email in a gmail account?
by jethro (Monsignor) on Feb 06, 2012 at 11:24 UTC

    The easiest way to make sure you process each message only once is to delete it when you fetch it from the server. Since POP does that by default I would assume this to be the more natural protocol for this.

    If you don't mind calling an external program for fetching (and are on linux or similar) you could use fetchmail.

Re: Most elegant way to process an email in a gmail account?
by locked_user sundialsvc4 (Abbot) on Feb 06, 2012 at 13:48 UTC

    What I would do, presuming that these are probably HTML-formatted emails anyway, is to put an arbitrary per-message hash-string into <--- an HTML comment --> within the message text.   If the e-mail is returned to you (by GMail or by any other mail server along the route), you can use the hash-string to verify that it (probably...) is a copy that was returned, and you can look up the information that you need (such as order number) in your own database. (!!)   You can also mark that particular message as “returned” since it is entirely possible that you will get more than one undeliverable-notice.   You can mark the e-mail address as “doubtful,” using the e-mail address that you stored, not the one listed in the body of the message you received.

    Also consider digitally signing every message (via S/MIME) so that you can vouch for its authenticity when processing it a second time.   These signatures are generally invisible even when the message is viewed on a web-mail screen.   (I personally think that every computer-generated e-mail should be signed, especially if, as in this case, you intend to react to response messages.   Never assume that the message you got, purporting to be an e-mail rejection message, is, in fact, an e-mail rejection message.   Never assume anything.)