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

时间:2019-01-09 09:18:06

标签: php mysql email gmail imap

我想从我的gmail帐户中获取电子邮件详细信息,然后保存到Mysql Table中。我想在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> ';

3 个答案:

答案 0 :(得分:1)

尝试此代码:

$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);

让我知道结果。

答案 1 :(得分:0)

尝试将''的{​​{1}}更改为

像这样:""

More info

答案 2 :(得分:0)

我有一个链接可以为您提供帮助

http://php.net/manual/en/function.imap-search.php

所有类型的条件和过滤器链接都可用

ALL - return all messages matching the rest of the criteria
ANSWERED - match messages with the \\ANSWERED flag set
BCC "string" - match messages with "string" in the Bcc: field
BEFORE "date" - match messages with Date: before "date"
BODY "string" - match messages with "string" in the body of the message
CC "string" - match messages with "string" in the Cc: field
DELETED - match deleted messages
FLAGGED - match messages with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set
FROM "string" - match messages with "string" in the From: field
KEYWORD "string" - match messages with "string" as a keyword
NEW - match new messages
OLD - match old messages
ON "date" - match messages with Date: matching "date"
RECENT - match messages with the \\RECENT flag set
SEEN - match messages that have been read (the \\SEEN flag is set)
SINCE "date" - match messages with Date: after "date"
SUBJECT "string" - match messages with "string" in the Subject:
TEXT "string" - match messages with text "string"
TO "string" - match messages with "string" in the To:
UNANSWERED - match messages that have not been answered
UNDELETED - match messages that are not deleted
UNFLAGGED - match messages that are not flagged
UNKEYWORD "string" - match messages that do not have the keyword "string"
UNSEEN - match messages which have not been read yet
$s_Date = "20 January 2018";
$b_Date = "10 December 2018"; 
$emails = imap_search($inbox,'SINCE "'.$s_Date.'" BEFORE"'.$b_Date.'"' );

现在您可以按照自己的意愿进行一些更改

如下所示:-

$emails = imap_search($inbox, 'NEW');