Another mail wrapper

#!/usr/bin/php -q
<?php
# implements sendmail wrapper
# by Petr Kutalek

if ($argc != 2)
{
  exit(1);
}
 
$message = '';
while(!feof(STDIN))
{
  $message .= fgets(STDIN, 4096);
}
 
$now = date('Y-m-d H:i:s');
$sender = $argv[1];
preg_match('/^To:.*([a-z0-9_\\-.]+@[a-z0-9_\\-.]+\\.[a-z]{2,4}).*$/miU', $message, $match); $recipient = $match[1];
 
if (!preg_match('/^[a-z0-9_\\-.]+@[a-z0-9_\\-.]+\\.[a-z]{2,4}$/i', $sender))
{
  exit(1);
}
 
$rvalue = 1;
 
$connect = mysql_connect('89.250.246.103', 'mail', 'chemuphuzUsp$8ew');
if (!$connect) exit(1);
$select = mysql_select_db('mail');
if (!$select) exit(1);
 
$result = mysql_query('DELETE FROM wrapmail WHERE timestamp < ' . date('U'));
$result = mysql_query("SELECT timestamp FROM wrapmail WHERE sender = '$sender'");
if (mysql_num_rows($result) <= 10)
{
  $h = popen("/usr/sbin/sendmail -i -F $sender -f $sender -- $recipient", 'w');
  fputs($h, $message);
  $rvalue = (pclose($h) >> 8) & 0xFF;
  $result = mysql_query("INSERT INTO wrapmail (timestamp, sender) VALUES (" . (date('U') + 3600). ", '$sender')");
}
 
mysql_close($connect);
exit($rvalue);
 
?>
mysql> describe wrapmail;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
| timestamp | int(11)          | NO   |     | 0       |                | 
| sender    | varchar(64)      | NO   |     |         |                | 
+-----------+------------------+------+-----+---------+----------------+
 
linux/apache/php/mailwrap.txt · Last modified: 2009/03/11 02:24 by admin