車両の位置を記載した電子メールが届き、データベースに位置を記録したいと考えています。メールを取得してコンテンツの一部を見つける方法を理解しました。ここで、まだ読んでいないメールのコンテンツのみを検索し、完了後に既読としてマークする方法を理解する必要があります。
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'myusername';
$password = 'mypassword';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'FROM "onlyoneemail@email.com"');
if($emails) {
$output = '';
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1);
if (preg_match('/\bAddress\s*:\s*(.+?)\s*$/mi', $message, $match)){
echo $match[1] . "<br>";
}
}
echo $output;
}
imap_close($inbox);