PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old March 25th, 2009, 07:30 AM
sassenach sassenach is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 76 sassenach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 31 m 41 sec
Reputation Power: 2
Email piping: stripping headers from message

Hi,

I have a email piping code show below.
It works fine, except that when i print the $message, it also shows me some headers.
How do i strip the headers from the message itself?

email_piping.php file
Code:
#!/usr/bin/php -q
<?php
//header('Content-Type: text/html; charset=utf-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  // always modified 
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache");                          // HTTP/1.0  
date_default_timezone_set('America/New_York');
ob_start();
require_once ('includes/config.inc.php');						  
require_once ('includes/functions.inc.php');
require_once ('includes/mysql_connect.php');
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

// handle email
$lines = explode("\n", $email);

// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$sendmail = "";
$splittingheaders = true;

//we have processed the headers and can start adding the lines to $message. 
for ($i=0; $i < count($lines); $i++) {
    if ($splittingheaders) {
        // this is a header
        $headers .= $lines[$i]."\n";

        // look out for special headers
        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
            $subject = $matches[1];
        }
        if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
            $from = $matches[1];
        }
    } else {
        // not a header, but message
        $message .= $lines[$i]."\n";
    }

    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $splittingheaders = false;
    }
	
	//$a = "RE: [119-24] Work Order Request - test - dsfsd";
	preg_match("/\[[0-9]*-[0-9]*\]/",$subject,$matches);
	list($jobid, $custid) = split('[-]', $matches[0]);
	$jobid = substr($jobid, 1);	//removes first string
	$custid = substr($custid, 0, (strlen($custid)-1));	//removes last string
}

$cur_date = returnLocalDate();
$cur_time = returnLocalTime();
$sendmail .= 'job id = '.$jobid.'<br/><br/>';
$sendmail .= 'customer id = '.$custid.'<br/><br/>';
$sendmail .= 'reply date = '.$cur_date.' '.$cur_time.'<br/><br/>';
$sendmail .= 'message = '.$message.'<br/><br/>';
//$sendmail .= 'from = '.$from.'\n';
//$sendmail .= 'subject = '.$subject.'\n';
//$sendmail .= 'headers = '.$headers.'\n';
//$sendmail .= 'message = '.$message.'\n';
mymailer('admin@milano4you.com','test pipe id ', $sendmail);
/*
$insertQ = mysql_query("INSERT INTO test_pipe(job_id, cust_id, description, reply_date, reply_time, displayed) 
						VALUES($jobid, $custid, '$message', '$cur_date', '$cur_time', 1)") or trigger_error("Query: $insertQ\n<br />MySQL Error: " .mysql_error());
if (mysql_affected_rows() > 0) {
	$new_reply_id = mysql_insert_id();
	echo $new_reply_id;
	$sendmail .= 'job id = '.$jobid.'\n';
	$sendmail .= 'customer id = '.$custid.'\n';
	$sendmail .= 'reply date = '.$cur_date.' '.$cur_time.'\n';
	$sendmail .= 'message = '.$message.'\n';
	$sendmail .= 'from = '.$from.'\n';
	$sendmail .= 'subject = '.$subject.'\n';
	$sendmail .= 'headers = '.$headers.'\n';
	$sendmail .= 'message = '.$message.'\n';
	mymailer('admin@milano4you.com','test pipe id '.$new_reply_id.'', $sendmail);
}
*/
return true;
if (isset($_SESSION['custfn'])){mysql_close();}
ob_end_flush();
?>


The email showed:
Quote:
job id = 119

customer id = 24

reply date = 2009-03-25 07:28

message = This is a multipart message in MIME format. ------=_NextPart_000_006F_01C9AD4D.7445AC60 Content-Type: multipart/alternative; boundary="----=_NextPart_001_0070_01C9AD4D.7445AC60" ------=_NextPart_001_0070_01C9AD4D.7445AC60 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 222Meyta fsdfsaasdsada email_logo E-mail: meytal@dachooch.com IL Phone: +972-(0)54-740-7505 IL Mobile: +972-(0)4-652-2704 USA Phone: +718-404-9369 Skype: dachooch2 www.dachooch.com ------=_NextPart_001_0070_01C9AD4D.7445AC60 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
222Meyta fsdfsaasdsada


Reply With Quote
  #2  
Old March 25th, 2009, 08:53 AM
jamestrowbridge jamestrowbridge is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2008
Location: Cleveland, Ohio, USA
Posts: 400 jamestrowbridge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 15 h 59 m 8 sec
Reputation Power: 2
This may be an SMTP server issue. My Exchange server was having some weird problems and some PHP that worked fine for about a year suddenly started including header info in the body - without changing it at all.
__________________
Sir, a desire of knowledge is the natural feeling of mankind; and every human being, whose mind is not debauched, will be willing to give all that he has to get knowledge.

Reply With Quote
  #3  
Old March 25th, 2009, 02:21 PM
sassenach sassenach is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 76 sassenach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 31 m 41 sec
Reputation Power: 2
so what can we do about it?

Reply With Quote
  #4  
Old March 25th, 2009, 03:10 PM
IAmALlama IAmALlama is offline
Me
Click here for more information. Click here for more information
Click here for more information
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 1,907 IAmALlama User rank is Private First Class (20 - 50 Reputation Level)IAmALlama User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 16 h 43 m 22 sec
Reputation Power: 4
I could be wrong when it comes to the email, but generally speaking new line characters have to be put inside of double quotes, not single. Single quotes will treat the newline character as a litteral \n where with double quotes it will convert it to an actual new line character.

Reply With Quote
  #5  
Old March 25th, 2009, 03:10 PM
jamestrowbridge jamestrowbridge is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2008
Location: Cleveland, Ohio, USA
Posts: 400 jamestrowbridge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 15 h 59 m 8 sec
Reputation Power: 2
First try some code that you are sure works - if it contains the headers, then its an SMTP issue. You will need to contact your SMTP server admin.

Reply With Quote
  #6  
Old March 26th, 2009, 04:16 AM
sassenach sassenach is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 76 sassenach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 31 m 41 sec
Reputation Power: 2
another thing, when i try to insert the "$message" in the DB, as such:
Code:
$insertQ = mysql_query("INSERT INTO test_pipe(job_id, cust_id, description, reply_date, reply_time, displayed) 
						VALUES($jobid, $custid, '$message', '$cur_date', '$cur_time', 1)") or trigger_error("Query: $insertQ\n<br />MySQL Error: " .mysql_error());


I get the following error:
Quote:
An error occurred in script '/home/milano4y/public_html/email_pipe.php' on line 77:
Query:
MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tab-interval:.5in'>


When i put just "hello world" instead of $message, it inserts just fine, so obviously it is that param that is not going through.
I am not sure why, but could it be because of the headers that is attached to it?
Any solutions welcomed.

Reply With Quote
  #7  
Old March 26th, 2009, 04:17 AM
sassenach sassenach is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 76 sassenach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 31 m 41 sec
Reputation Power: 2
by the way, i have aked my hosting regarding the SMTP issue, and i hope to get some answers soon. thanks

Reply With Quote
  #8  
Old March 26th, 2009, 08:13 AM
sassenach sassenach is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 76 sassenach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 31 m 41 sec
Reputation Power: 2
so my hosting company said its a scripting issue.
i searched the internet, but i cant seem to find anything.

any ideas?

Reply With Quote
  #9  
Old March 26th, 2009, 08:35 AM
jamestrowbridge jamestrowbridge is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2008
Location: Cleveland, Ohio, USA
Posts: 400 jamestrowbridge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 15 h 59 m 8 sec
Reputation Power: 2
I'll post up some code that works fine with my SMTP server when I get to work in a little while.

Regarding your SQL, try:

PHP Code:
 $sql "INSERT INTO test_pipe(job_id, cust_id, description, reply_date, reply_time, displayed) 
VALUES('"
.$jobid."', '".$custid."', '".$message."', '".$cur_date."', '".$cur_time."', '1')";
$insertQ mysql_query($sql) or trigger_error("Query: ".$sql."<br />MySQL Error: " .mysql_error()); 

Reply With Quote
  #10  
Old March 26th, 2009, 11:48 AM
jamestrowbridge jamestrowbridge is offline
Contributing User
Click here for more information.
 
Join Date: Jul 2008
Location: Cleveland, Ohio, USA
Posts: 400 jamestrowbridge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 15 h 59 m 8 sec
Reputation Power: 2
This works just fine with my server, so try it out on yours, just fill in the To, From, Subject and you can replace
Code:
<h2>HTML EMAIL</h2> 
with whatever your body should be. And if you expect recipients to be using email clients that do not support HTML you can edit:
Code:
This email has HTML in it and your email client doesn't support it, please visit the website here: http://www.test.com to see the email

To your liking.
PHP Code:
<html>
<
head>
<
title>
Mail Sent
</title>
</
head>
<
body>

<?
php

$today 
date("m-d-Y");
//Insert From Email Address Below Between single quotes
$fromwho '';

//Insert To Email Address Below Between single quotes
$to '';

//Insert Subject of Email Below Between single quotes
$subject '';

//Not really sure what this stuff is below, but it seems to work...
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash md5(date('r'time()));

//define the headers we want passed. Note that they are separated with \r\n 
$headers "From: ".$fromwho."\r\nReply-To: ".$fromwho."\r\n"

//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash?>

--PHP-alt-<?php echo $random_hash?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

This email has HTML in it and your email client doesn't support it, please visit the website here: http://www.test.com to see the email

--PHP-alt-<?php echo $random_hash?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>HTML EMAIL</h2>

--PHP-alt-<?php echo $random_hash?>-- 
--PHP-mixed-<?php echo $random_hash?>-- 
<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message ob_get_clean(); 
?>
<div align="right"><a href="javascript: self.close ()">Close this Window</a></div><hr />
<?php
//send the email 
$mail_sent = @mail$to$subject$message$headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent "<center>Mail sent</center>" "Mail failed";
?> 

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Email piping: stripping headers from message


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
Stay green...Green IT