Jump to content

Notice Undefined Offset


jmr3460

Recommended Posts

Hey all,

I have been working on a Blaclberry app that sends me an email with coord., timestamp and I can add some more. I have been able to open each of the emails with my new script. My goal is to insert the variables I have into a database. All this now is possible except I am getting a notice where there is an empty line of the email body.

The bodies of the email look like this:

Name: in

Latitude: XX.XXXXX

Longitude: -XX.XXXXXX

Altitude: 108m

Timezone:

Date: Apr 6, 2011 7:42 AM

Description: office

 

Address:

Address2:

City:

State:

Country:

Postal Code:

URL:

Phone Number:

 

Sent on the Sprint® Now Network from my BlackBerry®

The undefined offsets show up after Description and after Phone number.

 

This is what my script does:

 

Name: in

Latitude: 35.12063

Longitude: -89.95295

Altitude: 108m

Timezone:

Date: Apr 6, 2011 7:42 AM

Description: office

 

Notice: Undefined offset: 1 in /home3/simplic5/public_html/gps/gps.php on line 20

:

Address:

Address2:

City:

State:

Country:

Postal Code:

URL:

Phone Number:

 

Notice: Undefined offset: 1 in /home3/simplic5/public_html/gps/gps.php on line 20

:

 

This is my script:

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
//open database
$connection = imap_open("{mail.mailbox.com:143}INBOX", "mark@abc.com", "password");
    $message_number = imap_num_msg($connection);
    $headers = imap_headers($connection);
    $count = count($headers);
    for($i=1;$i<=$count;$i++){
$headerinfo = imap_base64(imap_body($connection, $i));

  	$message = nl2br($headerinfo);
  	$remove_2br = str_replace('<br />\n', '<br />', $message);
  	$messages = explode('<br />', $remove_2br);
  	$num_lines = (count($messages)-1);
for($a=0;$a< $num_lines;$a++){
//echo $messages[$a]."<br />";
$data = explode(': ',$messages[$a]);
$column = $data[0];
$field = $data[1];
echo $column.": ".$field."<br />\n";
//create sql to insert $field into $column
$sql = "";
}
}
?>

 

I think that the notice is generated because of the blank line in the email text. I only need about 4 or 5 bits of information.

Is there something I can do to delete the blank lines first, then I can process the rest of the email per the script?

Thanks for any help!

Mark

 

Link to comment
Share on other sites

Right, it'll do that every time there's a line without ": " in it, because explode() will only return an array with one member, so [1] doesn't exist. Here's a simple fix:

for($a=0;$a< $num_lines;$a++){
if(strpos($messages[$a], ": ") === false) continue;

By the way, if the lines are separated only by "\n" (or "\r\n"), why not just explode the string by that?

Link to comment
Share on other sites

Thanks your fix did it. The lines are coming from a blackberry browser and sent to my server via email. when I view the code there are <br /> at the end of every line. I just assumed the \n was there as well.

Anyway the next step is to create my database table and insert some data. Thanks very much for the help.

Mark

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.