Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 4th, 2003, 04:57 AM
Registered User
 
Join Date: Nov 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default webmail - with errors

these codes were from professional php 4, wrox

i have compiled it few times...

and in return, having two errors..

can someone explains?

webmail_class_test2.php

<?php

include "./webmail_class_ver2.php";

class My_Webmail extends Webmail
{
    function start()
    {
        $msgs = $this->getMsgList('readMsg', 'mailFrom');
        if (!$msgs) return false;
        $ret_str = '';
        foreach ($msgs as $msg)
            $ret_str .= $msg["subject"] . " - " . $msg["from"] . "<br>";
        return $ret_str;
    }
}

$host = "localhost";
$protocol = "imap";
$port = 143;
$userid = "chira";
$userpassword = "96422";
$mailbox = "INBOX";

$wmail = new My_Webmail();

if (!$wmail->init($host, $protocol, $port, $userid, $userpassword))
    echo($wmail->errorMsg());

$list = $wmail->start();

if (!$list)
    echo($wmail->errorMsg());
else
    echo($list);

$wmail->end();

?>


webmail_class_ver1.php

<?php

class Webmail
{
    var $host= '';
    var $protocol = 'imap';
    var $supported_protocols = array('imap', 'pop3');
    var $port = 143;
    var $userid = '';
    var $userpassword = '';

    var $stream = 0;
    var $mailbox = '';

    var $auto_expunge = true;

    var $ERR_ARGS_DELIMITER = " ";
    var $ERROR_MSG = '';

    var $ERR_STR_CONNECTION_FAILED = 'Connection failed!';
    var $ERR_STR_PROTOCOL_NOT_SUPPORTED = 'Protocol not supported!';
    var $ERR_STR_CLOSE_FAILED = 'Error closing the stream!';
    var $ERR_STR_MAILBOX_NOT_AVAILABLE = 'Mailbox not available!';
    var $ERR_STR_OVERRIDE_START = "Override start() method!";

    var $sort = 'SORTDATE';
    var $reverse = 0;

    var $ERR_STR_MAILBOX_STATUS_ERROR = 'Cannot get stat of mailbox!';
    var $STR_NO_SUBJECT = 'NO SUBJECT';
    var $STR_NO_FROM = 'UNKNOWN';


    function init($host, $protocol='imap', $port=143, $userid='', $userpassword='')
    {
        $this->host = $host;
        $this->protocol = $protocol;
        $this->port = $port;
        $this->userid = $userid;
        $this->userpassword = $userpassword;

        $this->mailbox = $GLOBALS["mailbox"];

        if (isset($GLOBALS["sort"])) $this->sort = $GLOBALS["sort"];
        if (isset($GLOBALS["reverse"])) $this->reverse = $GLOBALS["reverse"];

        if (!in_array($this->protocol, $this->supported_protocols))
        {
            $this->buildErrorMsg($this->ERR_STR_PROTOCOL_NOT_SUPPORTED,
                             $this->protocol);
            return false;
        }

        $mode = false;

        $this->stream = @imap_open("\{$this->host/$this->protocol:$this->port}$this->mailbox",
                              $this->userid, $this->userpassword, $mode);

        if (!$this->stream) {
            $this->buildErrorMsg($this->ERR_STR_CONNECTION_FAILED, imap_last_error());
            return false;
        }

        return true;
    }


    function start()
    {
        $this->buildErrorMsg($this->ERR_STR_OVERRIDE_START);
        return false;
    }


    function end()
    {
        if ($this->auto_expunge)
            $ret = @imap_close($this->stream, CL_EXPUNGE);
        else
            $ret = @imap_close($this->stream);

        if (!$ret){
            $this->buildErrorMsg($this->ERR_STR_CLOSE_FAILED, imap_last_error());
            return false;
        }

        return true;
    }


    function getMSgList($read_action, $mail_action)
    {
        $msgs = @imap_sort($this->stream, $this->sort, $this->reverse, SE_NOPREFETCH);

        if (!is_array($msgs))
            return false;

        for ($i = 0; $i < count($msgs); $i++){

            $msg = @imap_header($this->stream, $msg_no);
            $arr[$i]["no"] = $msg_no = $msgs[$i];
            $arr[$i]["uid"] = $msgs_uid = imap_uid($this->stream, $msg_no);

            if ($msg->Unseen == 'U' || $msg->Recent == 'R'){
                $arr[$i]["unseen"] = true;
            }
            else {
                $arr[$i]["unseen"] = false;
            }

            $arr[$i]["date"] = gmstrftime("%b %d %Y", strtotime($msg->date));

            $struct = imap_fetchstructure($this->stream, $msg_no);

            $num_parts = count($struct->parts) - 1;

            if ($num_parts > 0)
                $msg_prefix = "@";
            else
                $msg_prefix = '';

            if (empty($msg->subject)) {
                $arr[$i]["subject"] = $this->buildUrl("$read_action&msg_uid=$msg_uid&mailbox=$ this->mailbox", $this->STR_NO_SUBJECT);
            }
            else {
                $msg_subject = $this->decodeHeader($msg->subject);
                $arr[$i]["subject"] = $this->buildUrl("$read_action&msg_uid=$msg_uid&mailbox=$ this->mailbox", "$msg_prefix$msg_subject");
            }

            if (empty($msg->from)) {
                $arr[$i]["from"] = $this->STR_NO_FROM;
            }
            else {
                $arr[$i]["from"] = $this->makeAddress($msg->from, "$mail_action&msg_uid=$msg_uid&mailbox=$this->mailbox");
            }

        }
        return $arr;
    }


    function makeAddress($emails, $action)
    {
        if (!is_array($emails)) return;

        foreach ($emails as $email) {
            $personal = $this->decodeHeader($email->personal);
            $address = $email->mailbox . "@" . $email->host;

            if (!empty($personal)) {
                $arr[] = $this->buildUrl("action&email=$address", $personal);
            }
            else {
                $arr[] = $this->buildurl("action&email=$address", $address);
            }

        }

        return implode(',', $arr);
    }

    function decodeHeader($arg)
    {
        $dec_array = imap_mime_header_decode($arg);

        foreach ($dec_array as $obj)
            $arr[] = $obj->text;

        if (count ($arr) > 0)
            return implode('', $arr);
        else
            return $arg;
    }

    function buildUrl($option, $link, $onclick='')
    {
        global $PHP_SELF;

        if (!empty($on_click)) $onclick = " OnClick=\"$onclick\"";

        return "<a href=\"$PHP_SELF?$options\"$onclick>$link</a>";
    }


    function buildErrorMsg($err_msg, $err_arg='')
    {
        $this->ERROR_MSG = $err_msg . $this->ERR_ARGS_DELIMITER . $err_arg;
    }


    function errorMsg()
    {
        return $this->ERROR_MSG;
    }
}

?>

and errors are:

first time error:
Notice: Undefined variable: arr in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 138

second time error:
Notice: strtotime(): Called with empty time parameter in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 111

Notice: Undefined property: parts in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 115

Notice: Undefined variable: msg_uid in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 123

Notice: Undefined variable: options in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 181
NO SUBJECT - UNKNOWN

hope there is someone who can do something regarding these problems...
i cant figure out the solutiosn

Reply With Quote
  #2 (permalink)  
Old November 4th, 2003, 01:37 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I'm assuming that you have at least ONE typo in your post -- your errors are all in webmail_class_ver2.php, but your post says that we're looking at ver1.

Anyway, assuming that's just a typo, let's take a look at your first error. Undefined variable: arr on line 138. If you look at your function, $arr is never defined before it's used. You jump in the loop if $i < count($msgs) and start appending to the $arr array.

The first iteration of the loop happens when $i is zero... so what happens when $msgs is an empty array? You never add anything to $arr. Thus, $arr is never created, and you get the error on line 138 when you try to return $arr.

Second error, line 111: $arr[$i]["date"] = gmstrftime("%b %d %Y", strtotime($msg->date));

Well, if $msg->date isn't a valid time string, strtotime will complain about it. Nuff said, I guess... You should check to see if that string is nonempty before you pass it to a function that accepts something specific.

Rather than continue figuring out someone else's problem, I suggest the following: It seems to me that you're much better off reporting these problems to the author(s) of the class. They're much more familiar with it and might be actively supporting and maintaining it.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #3 (permalink)  
Old November 14th, 2003, 08:22 AM
Registered User
 
Join Date: Nov 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

im so sorry.. here is the ver2

webmail_class_ver2.php

<?php

class Webmail
{
    var $host= '';
    var $protocol = 'imap';
    var $supported_protocols = array('imap', 'pop3');
    var $port = 143;
    var $userid = '';
    var $userpassword = '';

    var $stream = 0;
    var $mailbox = '';

    var $auto_expunge = true;

    var $ERR_ARGS_DELIMITER = " ";
    var $ERROR_MSG = '';

    var $ERR_STR_CONNECTION_FAILED = 'Connection failed!';
    var $ERR_STR_PROTOCOL_NOT_SUPPORTED = 'Protocol not supported!';
    var $ERR_STR_CLOSE_FAILED = 'Error closing the stream!';
    var $ERR_STR_MAILBOX_NOT_AVAILABLE = 'Mailbox not available!';
    var $ERR_STR_OVERRIDE_START = "Override start() method!";

    var $sort = 'SORTDATE';
    var $reverse = 0;

    var $ERR_STR_MAILBOX_STATUS_ERROR = 'Cannot get stat of mailbox!';
    var $STR_NO_SUBJECT = 'NO SUBJECT';
    var $STR_NO_FROM = 'UNKNOWN';


    function init($host, $protocol='imap', $port=143, $userid='', $userpassword='')
    {
        $this->host = $host;
        $this->protocol = $protocol;
        $this->port = $port;
        $this->userid = $userid;
        $this->userpassword = $userpassword;

        $this->mailbox = $GLOBALS["mailbox"];

        if (isset($GLOBALS["sort"])) $this->sort = $GLOBALS["sort"];
        if (isset($GLOBALS["reverse"])) $this->reverse = $GLOBALS["reverse"];

        if (!in_array($this->protocol, $this->supported_protocols))
        {
            $this->buildErrorMsg($this->ERR_STR_PROTOCOL_NOT_SUPPORTED,
                             $this->protocol);
            return false;
        }

        $mode = false;

        $this->stream = @imap_open("\{$this->host/$this->protocol:$this->port}$this->mailbox",
                              $this->userid, $this->userpassword, $mode);

        if (!$this->stream) {
            $this->buildErrorMsg($this->ERR_STR_CONNECTION_FAILED, imap_last_error());
            return false;
        }

        return true;
    }


    function start()
    {
        $this->buildErrorMsg($this->ERR_STR_OVERRIDE_START);
        return false;
    }


    function end()
    {
        if ($this->auto_expunge)
            $ret = @imap_close($this->stream, CL_EXPUNGE);
        else
            $ret = @imap_close($this->stream);

        if (!$ret){
            $this->buildErrorMsg($this->ERR_STR_CLOSE_FAILED, imap_last_error());
            return false;
        }

        return true;
    }


    function getMSgList($read_action, $mail_action)
    {
        $msgs = @imap_sort($this->stream, $this->sort, $this->reverse, SE_NOPREFETCH);

        if (!is_array($msgs))
            return false;

        for ($i = 0; $i < count($msgs); $i++){

            $msg = @imap_header($this->stream, $msg_no);
            $arr[$i]["no"] = $msg_no = $msgs[$i];
            $arr[$i]["uid"] = $msgs_uid = imap_uid($this->stream, $msg_no);

            if ($msg->Unseen == 'U' || $msg->Recent == 'R'){
                $arr[$i]["unseen"] = true;
            }
            else {
                $arr[$i]["unseen"] = false;
            }

            $arr[$i]["date"] = gmstrftime("%b %d %Y", strtotime($msg->date));

            $struct = imap_fetchstructure($this->stream, $msg_no);

            $num_parts = count($struct->parts) - 1;

            if ($num_parts > 0)
                $msg_prefix = "@";
            else
                $msg_prefix = '';

            if (empty($msg->subject)) {
                $arr[$i]["subject"] = $this->buildUrl("$read_action&msg_uid=$msg_uid&mailbox=$ this->mailbox", $this->STR_NO_SUBJECT);
            }
            else {
                $msg_subject = $this->decodeHeader($msg->subject);
                $arr[$i]["subject"] = $this->buildUrl("$read_action&msg_uid=$msg_uid&mailbox=$ this->mailbox", "$msg_prefix$msg_subject");
            }

            if (empty($msg->from)) {
                $arr[$i]["from"] = $this->STR_NO_FROM;
            }
            else {
                $arr[$i]["from"] = $this->makeAddress($msg->from, "$mail_action&msg_uid=$msg_uid&mailbox=$this->mailbox");
            }

        }
        return $arr;
    }


    function makeAddress($emails, $action)
    {
        if (!is_array($emails)) return;

        foreach ($emails as $email) {
            $personal = $this->decodeHeader($email->personal);
            $address = $email->mailbox . "@" . $email->host;

            if (!empty($personal)) {
                $arr[] = $this->buildUrl("action&email=$address", $personal);
            }
            else {
                $arr[] = $this->buildurl("action&email=$address", $address);
            }

        }

        return implode(',', $arr);
    }

    function decodeHeader($arg)
    {
        $dec_array = imap_mime_header_decode($arg);

        foreach ($dec_array as $obj)
            $arr[] = $obj->text;

        if (count ($arr) > 0)
            return implode('', $arr);
        else
            return $arg;
    }

    function buildUrl($option, $link, $onclick='')
    {
        global $PHP_SELF;

        if (!empty($on_click)) $onclick = " OnClick=\"$onclick\"";

        return "<a href=\"$PHP_SELF?$options\"$onclick>$link</a>";
    }


    function buildErrorMsg($err_msg, $err_arg='')
    {
        $this->ERROR_MSG = $err_msg . $this->ERR_ARGS_DELIMITER . $err_arg;
    }


    function errorMsg()
    {
        return $this->ERROR_MSG;
    }
}

?>

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
webmail avik_u Javascript 0 October 31st, 2007 04:29 PM
Using Keyboard in the Webmail Application pannet1 CSS Cascading Style Sheets 1 March 26th, 2007 11:38 AM
SQLMail through webmail? vgv8 SQL Server 2000 0 December 2nd, 2004 10:29 AM
Can't get errors to display with <html:errors> michaeldill JSP Basics 0 August 2nd, 2004 01:47 PM
WebMail Project Help sidneyfuerte Classic ASP Professional 1 August 19th, 2003 10:04 AM



All times are GMT -4. The time now is 10:06 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.