Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Parsing E-Mail Headers
Old 07-15-2010, 03:07 PM Parsing E-Mail Headers
Red_X_'s Avatar
Extreme Talker

Posts: 158
Location: Houston
Trades: 0
I got a piping mail script setup to handle e-mails recieved.

When the e-mail is recieved it has all the header information.
Code:
From gmail  Thu Jul 15 14:01:19 2010
Received: from google  ([209.85.214.48]:62909)
        by mondial.websitewelcome.com  with esmtp (Exim 4.69)
        (envelope-from <gmail>)
        id 1OZTgN-0004e6-82
        for fake@acct.recycletheworld.org;  Thu, 15 Jul 2010 14:01:19 -0500
Received: by bwz2 with SMTP id 2so861660bwz.21
        for <fake@acct.recycletheworld.org>;  Thu, 15 Jul 2010 12:01:21 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com;  s=gamma;
        h=domainkey-signature:mime-version:received:received:date:message-id
         :subject:from:to:content-type;
        bh=cOegrnaRoip9DDzWIO25+fNukVhnAvr0dl9YdZwuJqU=;
        b=mxN9r6J9bMhJURwCZ8jTlUHgxu4BWfmHuLxHAjBbMMhphX5PtVLZ2sXzrrC52p6Iwk
         1mCNhFOmkOE1cjj5TM/DLzYUbnAuAXiyYrOVVKf1zUpO8rD5nnh8yG0Sx2c4rvDjErNT
         CvxgwsawdG21Zm+Q9UAmfdPPSEXZUL5hcxIuo=
DomainKey-Signature: a=rsa-sha1; c=nofws;
        d=gmail.com;  s=gamma;
        h=mime-version:date:message-id:subject:from:to:content-type;
        b=KfJIZXgvnM+cd1teEWuHNZhSBwUQP+qMuZsAVc9YdIBbEjazfBNxeeIxED695X7Xu2
         zjJ/vCVKhdMVlOeT7sK08ZX0ROOwONz1IxpLMoNgmel7kPFDDgg0v7LxuEouUqeMOrmy
         lQ6Qk8e+BiBNIiMto+W7XfHOBSSlHi2mNHv6w=
MIME-Version: 1.0
Received: by 10.204.178.68 with SMTP id bl4mr57579bkb.119.1279220480638;  Thu,
        15 Jul 2010 12:01:20 -0700 (PDT)
Received: by 10.204.66.73 with HTTP; Thu, 15 Jul 2010 12:01:20 -0700  (PDT)
PHP Code for email.php
PHP Code:
#!/usr/bin/php -q 
<?php
 
// read from stdin
$fd fopen("php://stdin""r");
$email "";
while (!
feof($fd))
{
    
$email .= fread($fd1024);
}
fclose($fd);
// handle email
$lines explode("\n"$email);

// empty vars
$from "";
$subject "";
$headers "";
$message "";
$splittingheaders false;
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;
    }
}
I don't want all that extra information. How would I go about doing this?
__________________
"Good News Everyone, by reading this your hearing my voice."
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
 
Register now for full access!
Old 07-15-2010, 07:43 PM Re: Parsing E-Mail Headers
dp_perdhanahost's Avatar
Experienced Talker

Posts: 47
Name: Dhanank Perdhana
Trades: 0
Hi Red X,

So, which part of the email do you want to have? Only the message body and drop all the header information?
__________________
# PERDHANAHOST Webhosting Services - Low cost business-ready webhosting
# Domain Registration - Shared Hosting - Reseller Hosting - Master Reseller Hosing
# Visit our website:
Please login or register to view this content. Registration is FREE
dp_perdhanahost is offline
Reply With Quote
View Public Profile Visit dp_perdhanahost's homepage!
 
Old 07-17-2010, 11:54 AM Re: Parsing E-Mail Headers
Red_X_'s Avatar
Extreme Talker

Posts: 158
Location: Houston
Trades: 0
Quote:
Originally Posted by dp_perdhanahost View Post
Hi Red X,

So, which part of the email do you want to have? Only the message body and drop all the header information?
Just the basic information of every e-mail. Subject, Message, From, To
__________________
"Good News Everyone, by reading this your hearing my voice."
Red_X_ is offline
Reply With Quote
View Public Profile Visit Red_X_'s homepage!
 
Old 07-17-2010, 01:13 PM Re: Parsing E-Mail Headers
dp_perdhanahost's Avatar
Experienced Talker

Posts: 47
Name: Dhanank Perdhana
Trades: 0
Hi Red_X_,

I made some modifications on your code. Hope it work for you. Printed in bold are modifications I made.

Code:
#!/usr/bin/php -q
<?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 = "";
$to = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
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];
        }
        if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
            $to = $matches[1];
        }
    } else {
        // not a header, but message
        $message .= $lines[$i]."\n";
    }

    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $splittingheaders = false;
    }
}

// these following lines only test if the code does it tasks or not.
echo "From: ". $from ."\n";
echo "To: ". $to ."\n";
echo "Subject: ". $subject ."\n";
echo "Message: ". $message ."\n";
Please be informed, that I commented this following line.

Code:
//$headers .= $lines[$i]."\n";
__________________
# PERDHANAHOST Webhosting Services - Low cost business-ready webhosting
# Domain Registration - Shared Hosting - Reseller Hosting - Master Reseller Hosing
# Visit our website:
Please login or register to view this content. Registration is FREE
dp_perdhanahost is offline
Reply With Quote
View Public Profile Visit dp_perdhanahost's homepage!
 
Reply     « Reply to Parsing E-Mail Headers
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.36998 seconds with 12 queries