Triggering LC scripts when emails are received

Alex Tweedly alex at tweedly.net
Wed Mar 19 19:49:49 EDT 2014


On 18/03/2014 09:04, jbv at souslelogo.com wrote:
> Then I made a test with an .irev script; the email forwarder also
> detected an error in the script, but was unable to fix it, so the
> test failed : the email bounced and was considered as spam.
> I guess I need to follow Matthias advice and ask on-rev the path
> to the LC engine on our server.
>
>
I had similar problem. Pending a reply from on-rev, I decided to be 
pragmatic and use a bit of php.

I modified my php test script to save the email to a file, and then 
trigger my LC script to operate on it.

#!/usr/bin/php -q
<?
/* Read the message from STDIN */
$fd = fopen("php://stdin", "r");
$email = ""; // This will be the variable holding the data.
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
/* Saves the data into a file to be processed by LC */
$fdw = fopen("./mail_cache.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);

/* Saves the data into a file to be a permanent log */
$fdw2 = fopen("./mail_all.txt", "w+");
fwrite($fdw2, $email);
fclose($fdw2);

$kick = file_get_contents('http://www.tweedly.org/testemail.lc/');
?>

and then I use an ordinary LC script to deal with it. Note that when I 
change to use LC directly, it will be simple to replace the section that 
reads the mail_cache file with reading from stdin - after that  the 
processing will be unaffected.

(Here's a trimmed down version of my LC script - remember the php script 
is at the top level, but the LC script is inside an html-folder, so it 
must use an absolute path to access the shared mail-cache file)

<?lc
set the errormode to inline

-- handlers for mail parsing
function parseMail pMail
    -- splits the email into the different parts (as an array)
    local K, tA, tCount, t, tStarted

    set the itemDel to ":"

    repeat for each line L in pMail
       if not tStarted then
          if L begins with "From " then
             put true into tStarted
          end if
          next repeat
       end if
       if char 1 of L = TAB then
          put CR & L after tA[K]
          next repeat
       end if
       if L contains ":" then
          put item 1 of L into K
          if K is among the keys of tA then
             put the keys of tA[K] into t
             replace CR with comma in t
             put max(t) into tCount
             add 1 to tCount
             put item 2 to -1 of L into tA[K][tCount]
          else
             put item 2 to -1 of L into tA[K]
          end if
       else
          put L & CR after tA["body"]
       end if
    end repeat
    return tA
end parseMail


put URL ("file:/home/myusername/mail_cache.txt") into tMail

put tMail after URL("file:./mail_handled.txt")

if tMail is empty then
   put "empty at" && the seconds  &CR after URL ("file:./lc_log.txt")
else
   put parseMail(tMail) into tAData
   put "Handled at " && the seconds && tAData["Message-ID"] &CR after 
URL ("file:./lc_log.txt")
end if
put empty into URL ("file:/home/myusername/mail_cache.txt")

put URL ("file:./lc_log.txt")

put " "
?>

-- Alex.




More information about the use-livecode mailing list