php - 通知 : Undefined property: stdClass: error

标签 php imap

我正在尝试获取电子邮件正文并根据它向学习者发送回复。但是在执行我的代码时出现错误 Notice: Undefined property: stdClass::$subject 。我真的很迷茫不知道该怎么办。我在下面发布代码。

<?php 

$server = '{imap.aaa.com:993/imap/ssl}INBOX';
$username = 'aaa@bbb.com';
$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('aaa@aaa.com' => 'name'))
  ->setTo(array('bbb@aaa.com'))
  ->setBody($message);


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

  }
}

else
{
   echo "false return";
}
?>

最佳答案

来自 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> ';
}

关于php - 通知 : Undefined property: stdClass: error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12830704/

相关文章:

java - JSP 中的 include 工作方式与 PHP 中的 include 类似吗?

ruby-on-rails - 在 ruby​​ 中支持 IMAP IDLE

php - imap_open : Can't open stream

php - 如何准备APPEND IMAP命令?

C# ImapX 2 电子邮件检查代码似乎很慢

php - MySQL注入(inject)查询

php - mysql 数据库规范化

php - symfony2.1 : count doctrine collection in twig template

php - 存储银行帐号

php - 如何启用 IMAP 模块