User email
Jump to navigation
Jump to search
Parsing email sent to a local user
Set up .forward
-bash-3.2# cat /export/home/user/.forward "|/usr/local/bin/php /export/home/user/phpmq.php"
Mail Queue Script
This takes all emails and puts them in ./mails folder organized by date.
<?php
$myemail = "mysuer@mydomain.com"; // sending errors here (DO NOT SEND TO LOCAL USER!)
while( $line = fread(STDIN, 1024))
{
$mail[] = $line;
}
$now = date("YmdHis");
$mailfolder = "mails/";
$filedone = 0;
//while file has not been written
while(!$filedone)
{
$randnum = rand(0, 1000000);
$file = $mailfolder . $now . "." . $randnum . ".mail";
if(file_exists($file))
{
sleep(1);
}
else
{
system("touch " . $file);
if(!$fh = fopen($file, 'a'))
{
//this is lazy
system("echo could not open file | mail $email");
}
foreach($mail as $key => $value)
{
fwrite($fh, $value);
}
fclose($fh);
$filedone = 1;
}
}
?
Parsing the emails
Run this script every x minutes in cron.
//load all the mails to a list
//note this is only for mails sent from a unix application. Special modifications to parsing may need to be done
//for emails from gmail/exchange/etc
$dir = "/export/home/user/mails/";
chdir($dir);
$handle = opendir($dir) or die("Could not open $dir");
//get all the current files in the directory
while(false !== ($file = readdir($handle)))
{
if((substr($file, 0, 1) != ".") and !(is_dir($file)))
{
$files[] = $file;
}
}
if(!count($files)) die();
foreach($files as $key => $filein)
{
$lines = Array();
$headers = Array();
$fullmsg = "";
$body = "";
//find out what the boundary is, add all lines to an array
$fp = fopen($dir . $filein, "r") or die("Could not open file $filein");
if($fp)
{
$bound_set = 0;
while(!feof($fp))
{
$in = fgets($fp);
$info = $in;
$fullmsg .= $info;
//if you dont need the headers, uncomment next line.
//continue;
//unix mail headers are done at the first line break
if($info == "\n")
{
$bound_set = 1;
}
if($bound_set && strlen(trim($info)))
{
//add to lines array
$lines[] = $info;
}
else
{
//get email headers
list($front, $back) = split(":", $info, 2);
$front = trim($front);
$back = trim($back);
if($front == "Subject")
{
$headers['subject'] = mysql_real_escape_string($back);
}
elseif($front == "From")
{
$headers['from'] = mysql_real_escape_string($back);
}
elseif($front == "Return-Path")
{
$headers['return'] = mysql_real_escape_string($back);
}
elseif($front == "To")
{
$headers['to'] = mysql_real_escape_string($back);
}
}
}
}
else
{
die("Could not open $filein");
}
//echo "x" . $headers['from'] . "x";
//print_r($lines);
//print_r($headers);
$body = mysql_real_escape_string(implode("", $lines));
$sub = $headers['subject'];
$from = $headers['from'];
$return = $headers['return'];
$to = $headers['to'];
$fullmsg = mysql_real_escape_string($fullmsg);
//do stuff with variables here
//move this email to an archive location.
/*
the following moves the email from the queue and puts it in the proper folder
at the time of this writing, it is:
$dir/complete/<year>/<month>/<day>/<mail_id>.mail
*/
$year = date("Y");
$newdir = $dir . "complete/" . $year;
if(!is_dir($newdir))
{
system("mkdir $newdir");
}
$month = date("m");
$newdir = $newdir . "/" . $month;
if(!is_dir($newdir))
{
system("mkdir $newdir");
}
$day = date("d");
$newdir = $newdir . "/" . $day;
if(!is_dir($newdir))
{
system("mkdir $newdir");
}
$logdir = $newdir . "/" . $id . ".mail";
$query = "insert into audit values(NULL, '$id', NULL, '$logdir')";
mysql_query($query) or die("Could not add log.\n");
$str = "mv $dir" . "$filein" . " " . $newdir . "/" . $id . ".mail";
system($str);
}
?>