Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notice: Undefined property: stdClass: error

Tags:

php

imap

I am trying to fetch an email body and send a reply to the learner based on it. But i am getting an error Notice: Undefined property: stdClass::$subject while executing my code. I am really lost not knowing what to do. I am posting the code below.

<?php 

$server = '{imap.aaa.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'aaaaaa';
require_once '../swift/lib/swift_required.php';

$connection  = imap_open($server,$username,$password) or die('Cannot connect to Gmail: ' .    imap_last_error());

$result = imap_search($connection,'ALL');
if($result) {


  $output = '';


  rsort($result);


  foreach($result as $email_number) {

    $overview = imap_fetch_overview($connection,$email_number,0);
    $message = imap_fetchbody($connection,$email_number,2);

    $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
    $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
    $output.= '<span class="from">'.$overview[0]->from.'</span>';
    $output.= '<span class="date">on '.$overview[0]->date.'</span>';
    $output.= '</div>';

    $output.= '<div class="body">'.$message.'</div>';

  if ($message == signup)

  {

$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);

$message1 = Swift_Message::newInstance('new message')
  ->setFrom(array('[email protected]' => 'name'))
  ->setTo(array('[email protected]'))
  ->setBody($message);


  $mail = $mailer->send($message1);
  }

  }
}

else
{
   echo "false return";
}
?>
like image 809
faz Avatar asked Oct 11 '12 01:10

faz


1 Answers

From the documentation for imap_fetch_overview:

Returns an array of objects describing one message header 
each. The object will only define a property if it exists.

So check if the property is set first:

if (isset($overview[0]->subject))
{
   $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
}
like image 185
Geoff Montee Avatar answered Nov 15 '22 21:11

Geoff Montee