2011-12-29 3 views
1

w3schools 포럼의 사용자는 IMAP 기능을 사용하여 개인 메일 서버의 메일받은 편지함을 검사하고 내가 좋아하는 일을 수행 할 수있는 몇 가지 코드를 알려주었습니다. 전자 메일 내용을 MySQL 테이블에 게시합니다.PHP IMAP을 사용하여 전자 메일 열기

전자 메일받은 편지함을 열어받은 편지함에서 전자 메일을 확인하는 방법에 대한 해결책을 찾는 사람이 있습니까? (이전 전자 메일이 자동으로 삭제되기 때문에 하나만있을 것입니다. mail 메시지를 $ open_email_msg로 설정하십시오. MySQL 테이블에 전자 메일을 게시하기위한 일련의 명령을 시작한 다음 전자 메일을 삭제하고받은 편지함을 닫으시겠습니까?

다음 사람이 나를 도와주는 코드입니다.

<?php 

$now = time(); // current time 

$mailbox = '{192.168.150.11:143/imap/novalidate-cert}'; // see http://www.php.net/manual/en/function.imap-open.php 
$mbox = imap_open($mailbox, 'username', 'password'); // log in to mail server 

if (!$mbox) 
    echo ('Failed opening mailbox<br>' . print_r(imap_errors(), true)); // remove the print_r for production use 
else 
{ 
    $box = imap_check($mbox); // get the inbox 

    for ($imap_idx = 1; $imap_idx <= $box->Nmsgs; $imap_idx++) // loop through the messages 
    { 
    $headers = imap_headerinfo($mbox, $imap_idx); // http://www.php.net/manual/en/function.imap-headerinfo.php 
    $raw_headers = imap_fetchheader($mbox, $imap_idx); // http://www.php.net/manual/en/function.imap-fetchheader.php 
    $selected_headers = ''; 
    $text_part = ''; 
    $html_part = ''; 
    $original_message = imap_body($mbox, $imap_idx); // save the copy of the entire thing, attachments and all 

    // build selected headers string 
    for ($ii = 0; $ii < count($headers->from); $ii++) 
     $selected_headers .= 'From: ' . $headers->from[$ii]->mailbox . '@' . $headers->from[$ii]->host . "\n"; 
    for ($ii = 0; $ii < count($headers->to); $ii++) 
     $selected_headers .= 'To: ' . $headers->to[$ii]->mailbox . '@' . $headers->to[$ii]->host . "\n"; 
    for ($ii = 0; $ii < count($headers->cc); $ii++) 
     $selected_headers .= 'Cc: ' . $headers->cc[$ii]->mailbox . '@' . $headers->cc[$ii]->host . "\n"; 
    for ($ii = 0; $ii < count($headers->bcc); $ii++) 
     $selected_headers .= 'Bcc: ' . $headers->bcc[$ii]->mailbox . '@' . $headers->bcc[$ii]->host . "\n"; 
    if (!empty($headers->date)) 
     $selected_headers .= 'Date: ' . $headers->date . "\n"; 
    if (!empty($headers->subject)) 
     $selected_headers .= 'Subject: ' . $headers->subject . "\n"; 



    // see below; getMsg uses global variables 
    getMsg($mbox, $imap_idx); 

    $text_part = $plainmsg; // text portion of the email 
    $html_part = $htmlmsg; // html portion of the email 

    // check for text portion first 
    $msg_text = trim(strip_tags($plainmsg 

답변

0

이메일을 읽을 수있는이 코드를 사용해보십시오.

 $username=$email; // e.g. [email protected] 
     $password='password'; 

     $hostname = '{example.com:995/pop3/ssl/novalidate-cert}'; 
     $username = $username; $password = $password; $imap = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error()); 

      $message_count = imap_num_msg($imap); 
      // print $message_count; 


    for ($i = 1; $i <= $message_count; ++$i) 
      { 
       $header = imap_header($imap, $i); 
       $body = imap_fetchbody($imap, $i,2); 
       $prettydate = date("jS F Y", $header->udate); 

       if (isset($header->from[0]->personal)) { 
        $personal = $header->from[0]->personal; 
       } else { 
        $personal = $header->from[0]->mailbox; 
       } 

     $subject=$header->Subject; 

    $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>"; 
       echo "On $prettydate, $email said \"$body\".\n"; 
       echo '<br><br>'; 
      } 

     print_r(imap_errors()); 
     imap_close($imap); 
} 
0

this link이 내가 직접 사용했기 때문에 도움이 될 것입니다. 그리고 이것이 저에게 잘 작동하고 있습니다.

코드를 등록하고 다운로드 할 수 있으며 간단합니다.

또는 당신이 헤더의 정보를 얻으려면 당신이 할 수있는 것은 아니라 :

$mbox = imap_open("{[email protected]:995/pop3/ssl/novalidate-cert}INBOX", '[email protected]', 'pass') 
       or die("can't connect: " . imap_last_error()); 


$MC = imap_check($mbox); 
    $result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0); 

    foreach ($result as $overview) { 
    echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from} 
    {$overview->subject}\n"; 
    echo "<br>"; 
} 
관련 문제