0

Gmailアカウントから未読メールを取得する方法を知りたい. Gmail アカウントを持っていますが、Gmail アカウントからのすべての未読メールが PHP を介して自分のサイトに届くようにしたいと考えています。さまざまなコードを実装していますが、役に立ちません。この問題を解決するのを手伝ってください。

gmail からメールを取得するために使用しているコードがありますが、メールが取得されません。

<?php 
/* connect to gmail */

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'abc@gmail.com';
echo $password = 'abcd';

/* try to connect */
$inbox =  imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
 echo "got inbox";
/* grab emails */
$emails = imap_search($inbox);

/* if emails are returned, cycle through each... */
if($emails) {
  echo "got emails";
  /* begin output var */
  $output = '';

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

  /* for every email... */
  foreach($emails as $email_number) {
    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);
    $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[0]->subject.'</span> ';
    $output.= '<span class="from">'.$overview[0]->from.'</span>';
    $output.= '<span class="date">on '.$overview[0]->date.'</span>';
    $output.= '</div>';

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

  echo $output;
} 

/* close the connection */
imap_close($inbox);
?>
4

2 に答える 2

2

未読メッセージを取得するには、次のようにします。

$inbox =  imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$unread_msgs = imap_search($inbox, 'UNSEEN');

他のオプションについては、対応するマニュアルも参照できます。

于 2012-10-31T11:08:09.163 に答える
1

使用imap_open:

$inbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX",$username,$password);

ここで例を参照してください。

于 2012-10-31T10:58:08.913 に答える