php 注意:未定义的属性:stdClass:错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12830704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:18:26  来源:igfitidea点击:

Notice: Undefined property: stdClass: error

phpimap

提问by faz

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::$subjectwhile executing my code. I am really lost not knowing what to do. I am posting the code below.

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

<?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";
}
?>

回答by Geoff Montee

From the documentation for imap_fetch_overview:

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