Page 1 of 1

list() and explode() help needed.

Posted: Fri Nov 12, 2010 11:09 pm
by spedula
Okay, So i've been hacking away at this bounced email parsing script for 6 hours straight now.

I need away to extract a variable from the header of the bounced email. The email format is this:

user-VARIABLE I NEED-bounce@domain.com

I am able to get the bounced emails piped correctly to the script and it updates mySQL correctly when I run the script with a $_GET var in the URL.

This is the code I'm using to try to extract my variable from the email address. I'm almost 100% certain that this is the culprit in all this.

Code: Select all

list($part1,$dum1) = explode("-bounce@domain.com", trim($to) ); 
list($dum2,$unsublink) = explode("user-", $part1);
Just in case thats not the issue, here is the whole parser.

Code: Select all

// Reading in the email
$fd = fopen("php://stdin", "r");
while (!feof($fd)) {
  $email .= fread($fd, 1024);
}
fclose($fd);

// Parsing the email
$lines = explode("\n", $email);
$stillheaders=true;
for ($i=0; $i < count($lines); $i++) {
  if ($stillheaders) {
    // 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
    break;
    // Optionally you can read out the message also, instead of the break:
    //$message .= $lines[$i]."\n";
  }

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

list($part1,$dum1) = explode("-bounce@domain.com", trim($to) ); 
list($dum2,$unsublink) = explode("user-", $part1);

Any help here is appreciated. I'm about to start ripping hair out... :banghead:

Re: list() and explode() help needed.

Posted: Fri Nov 12, 2010 11:13 pm
by requinix
Assuming the variable doesn't have a '-' in itself,

Code: Select all

$to = "user-VARIABLE I NEED-bounce@domain.com";
list(, $variable) = explode("-", $to);
but what you have should do it too.
spedula wrote:I'm almost 100% certain that this is the culprit in all this.
Culprit in what?

Re: list() and explode() help needed.

Posted: Fri Nov 12, 2010 11:16 pm
by spedula
Culprit in not giving me the variable that I need.

I used:

Code: Select all

mail($email, 'Bounced emails', $unsublink, $headers);
to send myself a copy of the variable $unsublink. It keeps getting sent back blank and the DB is not getting updated.