list() and explode() help needed.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

list() and explode() help needed.

Post 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:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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?
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

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

Post 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.
Post Reply