proj2501
03-04-2005, 06:24 PM
I am using the script below to insert email into a database. I've made a few slight modifications that are a bit ugly but the script works for the most part and they will do for now. Right now my main issue is with the way email clients format mail. If I send mail from the command line everything shows up in the database correctly. If I send using Outlook or Outlook Express it enters it with all of the enconding information. What do I need to do so that the email is inserted in the proper format?
#!/usr/local/bin/php
<?php
$stdin = fopen("php://stdin", "r");
/* This is used for bounced emails. */
$support_email = "";
$support_name = "";
/* make the mysql connection */
$connection=mysql_connect('', '', '');
$db=mysql_select_db('',$connection);
/* read in the pipe from stdin */
while (!feof($stdin)) {
$buffer = fgets($stdin, 4096);
$message[] .= $buffer;
}
/* Get the email headers */
foreach($message as $header_build) {
if (preg_match("/^$/", $header_build)) break;
$header_build = preg_replace("/:\s/", ":", $header_build);
if (preg_match("/:/", $header_build)) {
$vars = preg_split("/:/", $header_build, 2);
if ($vars[1]) {
chop($header[$vars[0]] = $vars[1]);
}
}
}
/* strip out Re:'s in subject */
if ($header['Subject']) {
$header['Subject'] = preg_replace("/\s*Re:\s*/", "", $header['Subject']);
}
/* initialize Cc: header */
if (!$header['Cc']) {
$header['Cc'] = "";
}
/* fix empty subject header */
$header['Subject']=preg_replace("/\n/","",$header['Subject']);
if ($header['Subject']=="") {
$header['Subject'] = "{no subject}";
}
$from = mysql_escape_string($header['From']);
$x=strlen($from);
$from[$x]='';
$from[$x-1]='';
$from[$x-2]='';
$from[$x-3]='';
$x=0;
while($from[$x]!='<'){
$from[$x]='';
$x++;
}$from[$x]='';
$from=trim($from);
$sql=mysql_query("SELECT * FROM users WHERE email_address='$from'");
$x=mysql_num_rows($sql);
if($x==0){
$subject="Unable to deliver your message";
$email_body="The email address used to send your message is not subscribed to this site. If you are a member of the site, please be aware that you may only send messages using the email address you have registered with.\r\n";
$email_body.="If you would like to join, please visit";
$email_body.="\r\n$from";
mail($from,"$subject","$email_body",$support_email);
}else{
$cc = mysql_escape_string($header['Cc']);
$subject = mysql_escape_string($header['Subject']);
if(strlen($cc)==0){
$x=3;
}else{
$x=5;
} /* Get the message body */
for($i = count($header) + $x; $i <= count($message); $i++) {
$body .= $message[$i];
}if (preg_match("/^\n$/",$body)) {
$body = "[empty message body]";
} else {
$body = mysql_escape_string($body);
}if ($attachment>0) {
$body="[message contained one or more attachments]";
}$query = "insert into articles (author, cc, subject, body, date, time) ";
$query .= "values ('$from', '$cc', '$subject', '$body', now(), now())";
mysql_query($query);
}
?>
#!/usr/local/bin/php
<?php
$stdin = fopen("php://stdin", "r");
/* This is used for bounced emails. */
$support_email = "";
$support_name = "";
/* make the mysql connection */
$connection=mysql_connect('', '', '');
$db=mysql_select_db('',$connection);
/* read in the pipe from stdin */
while (!feof($stdin)) {
$buffer = fgets($stdin, 4096);
$message[] .= $buffer;
}
/* Get the email headers */
foreach($message as $header_build) {
if (preg_match("/^$/", $header_build)) break;
$header_build = preg_replace("/:\s/", ":", $header_build);
if (preg_match("/:/", $header_build)) {
$vars = preg_split("/:/", $header_build, 2);
if ($vars[1]) {
chop($header[$vars[0]] = $vars[1]);
}
}
}
/* strip out Re:'s in subject */
if ($header['Subject']) {
$header['Subject'] = preg_replace("/\s*Re:\s*/", "", $header['Subject']);
}
/* initialize Cc: header */
if (!$header['Cc']) {
$header['Cc'] = "";
}
/* fix empty subject header */
$header['Subject']=preg_replace("/\n/","",$header['Subject']);
if ($header['Subject']=="") {
$header['Subject'] = "{no subject}";
}
$from = mysql_escape_string($header['From']);
$x=strlen($from);
$from[$x]='';
$from[$x-1]='';
$from[$x-2]='';
$from[$x-3]='';
$x=0;
while($from[$x]!='<'){
$from[$x]='';
$x++;
}$from[$x]='';
$from=trim($from);
$sql=mysql_query("SELECT * FROM users WHERE email_address='$from'");
$x=mysql_num_rows($sql);
if($x==0){
$subject="Unable to deliver your message";
$email_body="The email address used to send your message is not subscribed to this site. If you are a member of the site, please be aware that you may only send messages using the email address you have registered with.\r\n";
$email_body.="If you would like to join, please visit";
$email_body.="\r\n$from";
mail($from,"$subject","$email_body",$support_email);
}else{
$cc = mysql_escape_string($header['Cc']);
$subject = mysql_escape_string($header['Subject']);
if(strlen($cc)==0){
$x=3;
}else{
$x=5;
} /* Get the message body */
for($i = count($header) + $x; $i <= count($message); $i++) {
$body .= $message[$i];
}if (preg_match("/^\n$/",$body)) {
$body = "[empty message body]";
} else {
$body = mysql_escape_string($body);
}if ($attachment>0) {
$body="[message contained one or more attachments]";
}$query = "insert into articles (author, cc, subject, body, date, time) ";
$query .= "values ('$from', '$cc', '$subject', '$body', now(), now())";
mysql_query($query);
}
?>