php - 根据 UID 从 Gmail 获取电子邮件数据

标签 php mysql email gmail imap

我想从我的 Gmail 帐户获取电子邮件详细信息并保存到 Mysql 表中。我想根据 UID 获取电子邮件。但我没有得到所需的结果。谁能告诉我当我的 gmail 帐户收到新电子邮件时如何获取电子邮件、主题和正文。这是我的代码:

/* connect to server */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'myemail';
$password = 'mypassword';

/* try to connect */
$inbox = imap_open($hostname,$username,$password,OP_READONLY) or die('Cannot connect to Tiriyo: ' . imap_last_error());
//echo $inbox;
/* grab emails */
//$emails = imap_search($inbox,'ALL');
$latest_uid = 16; // Last UID inserted in my table 

$emails   = imap_search($inbox, 'ALL', SE_UID);
print_r($emails);
/* if emails are returned, cycle through each... */
if($emails) {

  /* begin output var */
  $output = '';

  /* put the newest emails on top */
  rsort($emails);

  /* for every email... */
  foreach($emails as $email_number) {
    //$email_number=$emails[0];
//print_r($emails);
    /* get information specific to this email */
    $message = imap_fetchbody($inbox,$email_number, 1);
    //echo $message;
    $overview = imap_fetch_overview($inbox,"1:{$email_number}");
   // $message = imap_fetchbody($inbox,$email_number,2);

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


     $output.= '<span class="from">'.$overview->uid.'</span>';

    $output.= '</div>';

    /* output the email body */
    $output.= '<div class="body">'.$message.'</div>';
  }

  echo $output;
}

/* close the connection */
imap_close($inbox);

我收到错误:

Notice: Trying to get property of non-object in C:\xampp\htdocs\imap\custom\index2.php on line 37

Notice: Trying to get property of non-object in C:\xampp\htdocs\imap\custom\index2.php on line 38

Notice: Trying to get property of non-object in C:\xampp\htdocs\imap\custom\index2.php on line 41

这是第 37 行:

$output.= '<span class="subject">'.$overview->subject.'</span> ';

最佳答案

试试这个代码:

$mbox = imap_open("Hostname", "username", "password")
     or die("can't connect: " . imap_last_error());

$MC = imap_check($mbox);

// Fetch an overview for all messages in INBOX
$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
foreach ($result as $overview) {
    echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from}
    {$overview->subject}\n";
}
imap_close($mbox);

让我知道结果。

关于php - 根据 UID 从 Gmail 获取电子邮件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54106667/

相关文章:

php - 在 Xampp 中将 Gmail 配置为 SMTP 以发送邮件 - 出现错误。为什么?

php - 如果第二个数组具有与 PHP 匹配的 ID 键/值,则将数据添加到数组

php - 如何插入带有列值自动增量 pk 值的记录?

php - 根据结果​​通过电子邮件发送 PHP

php - 从 mysql 中的一对多连接中提取最新结果

PHP函数每次将变量递增1

mysql - Joomla安装错误INSTL_ERROR_INITIALISE_SCHEMA

php - 拉拉维尔 : user defined object name in Join Query

ios - MFMailComposeViewController 发送按钮禁用

php - php mail() 是群发邮件的好选择吗?