Notice: Undefined property: stdClass: error
Asked Answered
S

1

8

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";
}
?>
Sanbenito answered 11/10, 2012 at 1:2 Comment(2)
The error message seems quite obvious, $overview[0]->subject is not defined :) so var_dump($overview[0]) would be the next course of action.Deaton
But I am wondering why define only the $overview[0]->subject. When there are more similar data like from and date?Sanbenito
P
19

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> ';
}
Perimorph answered 11/10, 2012 at 1:8 Comment(1)
sorted out. Thank you. This particular mail didn't have a subject.Sanbenito

© 2022 - 2024 — McMap. All rights reserved.