Welcome Guest, Not a member yet? Register   Sign In
"Severity: Notice" from custom Library
#1

[eluser]chadwhitaker[/eluser]
Hello,

I'm creating a Codeigniter Library from a PHP class I found. The class is used to obtain emails from an email server.

Here is part of my Library:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Emailreader {
        
    //Constructor
    function Emailreader ($config = array())
    {
        
        $this->server     =     '{'.$config['mailserver'].':'.$config['port']. '/'.$config['servertype'].''.($config['ssl'] ? "/ssl" : "").''.($config['cert'] ? "" : "/novalidate-cert").'}INBOX';
        $this->username    =    $config['username'];
        $this->password    =    $config['password'];
        
    }
    
    
    //Make connection to mail server
    function open_mailbox()
    {
        $this->mailbox = @imap_open($this->server, $this->username, $this->password);
        
        if(!$this->mailbox)
        {
            echo 'Error: Can not connect to mail server.' . "\n\n";
            exit;
        }
    }
    
    

    // Get Header info from email
    // **##**##**##**## THIS METHOD IS WHERE THE ERROR IS COMING FROM **##**##**##**##**##**##**##**##**##**##**##**##
    function headers($message_id)
    {
        if(!$this->mailbox)
            return false;

        $mail_header = imap_headerinfo($this->mailbox, $message_id);
        
        $sender = $mail_header->from[0]; //create sender array
        
        if(strtolower($sender->mailbox) != 'mailer-daemon' && strtolower($sender->mailbox) != 'postmaster')
        {
            $mail_details = array(
                    //For more: http://www.php.net/manual/en/function.imap-headerinfo.php
                    
                    'to' => strtolower($mail_header->toaddress),
                    'from' => strtolower($sender->mailbox).'@'.$sender->host,
                    'subject' => $mail_header->subject,
                    'date' => $mail_header->date,
                    'maildate' => $mail_header->MailDate,
                );
        }
        return $mail_details;
    }
    
}
?>

Now using this Library I'm able to grab data from an emails header like the "to", "from", or "subject" using the headers() method.

But if an email doesn't have a subject... then my controller will return an error for that email:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: stdClass::$subject

Filename: libraries/Emailreader.php

Is there a way to prevent this? I know this is something that doesn't happen with the Emailreader class using just PHP (without Codeigniter)

Thank you!
#2

[eluser]cideveloper[/eluser]
Can you do a

Code:
print_r($mail_header);

so we can see the resulting array. That way we can see what is going on with $mail_header->subject

Edit: Try the below code.

Code:
if(strtolower($sender->mailbox) != 'mailer-daemon' && strtolower($sender->mailbox) != 'postmaster')
{
    $subject = (isset($mail_header->subject)) ? $mail_header->subject : "";
    $mail_details = array(
        'to' => strtolower($mail_header->toaddress),
        'from' => strtolower($sender->mailbox).'@'.$sender->host,
        'subject' => $subject,
        'date' => $mail_header->date,
        'maildate' => $mail_header->MailDate,
    );
}
#3

[eluser]chadwhitaker[/eluser]
This is the output with a test email sent without a subject.

Code:
stdClass Object ( [date] => Tue, 11 Jan 2011 13:29:19 -0500
[Date] => Tue, 11 Jan 2011 13:29:19 -0500
[message_id] =>
[toaddress] => [email protected]
[to] => Array ( [0] => stdClass Object ( [mailbox] => paychecker+chad [host] => cwhitaker.com ) )
[fromaddress] => Chad Whitaker
[from] => Array ( [0] => stdClass Object ( [personal] => Chad Whitaker [mailbox] => chad [host] => cwhitaker.com ) )
[reply_toaddress] => Chad Whitaker
[reply_to] => Array ( [0] => stdClass Object ( [personal] => Chad Whitaker [mailbox] => chad [host] => cwhitaker.com ) ) [senderaddress] => Chad Whitaker
[sender] => Array ( [0] => stdClass Object ( [personal] => Chad Whitaker [mailbox] => chad [host] => cwhitaker.com ) )
[Recent] =>
[Unseen] => U
[Flagged] =>
[Answered] =>
[Deleted] =>
[Draft] =>
[Msgno] => 1
[MailDate] => 11-Jan-2011 18:29:41 +0000
[Size] => 1787
[udate] => 1294770581 )

EDIT:
So if the subject is missing from the email, the [subject] will not be created in the array.
#4

[eluser]chadwhitaker[/eluser]
That does prevent the error, and I can see how & why.

But it there a way around this? Say I wanted to add on to this Library and have all the headers available to me. I don't want an error for every part of the array that is missing, nor have to use that code for every single array?


EDIT:
For example.. if I wanted to pull the "CC" from an email, but every email will not be cc'd. I would have to add the same script you used, and it will bloat this script a lot.

Does that make sense?




Theme © iAndrew 2016 - Forum software by © MyBB