php - 根据 UID 从 Gmail 获取电子邮件数据
问题描述
我想从我的 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);
让我知道结果。
推荐阅读
- r - 如何在不使用 for 循环的情况下优化我的 R 代码
- javascript - 为什么我的数据表固定标题不起作用/
- python - conda 卸载和安装时软件包不匹配
- hive - 如何在 Hive 的当前层中使用查询结果?
- python - 如何启动 Python 游戏循环?
- autodesk-forge - Blazor webassembly http 默认标头 Forge Design Automation
- python - django.db.utils.ProgrammingError:关系“blog_category”不存在
- html - 如何防止我的桌子随着滚动而移动以及如何使其居中?
- php - 无法通过 PHP 中的 Here Map API 生成令牌
- python - 计算具有两个层次结构的数据的 14 天滚动平均值