jmr3460 Posted April 7, 2011 Share Posted April 7, 2011 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", "[email protected]", "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 Quote Link to comment https://forums.phpfreaks.com/topic/232925-notice-undefined-offset/ Share on other sites More sharing options...
dcro2 Posted April 7, 2011 Share Posted April 7, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232925-notice-undefined-offset/#findComment-1197995 Share on other sites More sharing options...
jmr3460 Posted April 7, 2011 Author Share Posted April 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/232925-notice-undefined-offset/#findComment-1198018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.