Jump to content

Retrieving Email Using PHP


gaza165

Recommended Posts

I have a class that uses the imap extention. Here is the code for it:

 

<?php
//if (!extension_loaded("imap")) {
// dl("php_imap.dll");
//}

class GetMail {
  private $host;
  private $user;
  private $pass;  
  private $type;
  private $mbox;
  private $port;
  private $mh = null;
  
  function __construct($host, $user, $pass, $type = 'imap', $mbox = 'INBOX', $port = null) {
    $this->host = $host;
    $this->user = $user;
    $this->pass = $pass;
    $this->type = strtolower($type);
    if (is_null($port)) {
      switch($this->type) {
        case 'imap' :
          $this->port = 143;
          break;
        case 'pop3' :
          $this->port = 110;
          break;
        case 'nntp' :
          $this->port = 119;
          break;
      }
    }
    else {
      $this->port = $port;
    }
    $this->mbox = $mbox;
    $mailbox = "$this->host:$this->port/$this->type";
    $this->mh = imap_open("{" . $mailbox . "}$this->mbox", $this->user, $this->pass, 16);
  }
  
  function __destruct() {
    if ($this->mh) {
      imap_close($this->mh);
    }
  }
  
  function num_msg() {
    return imap_num_msg($this->mh);
  }

  function num_recent() {
    return imap_num_recent($this->mh);
  }
  
  function headers($offset = 1, $max = 0) {
    $msg_from = $offset;
    if ($max > 0) {
      $msg_to = min($max + $offset, $this->num_msg());
    }
    else {
      $msg_to = $this->num_msg();
    }
    $headers = array();
    for ($i = $msg_from; $i <= $msg_to; $i++) {
      $headers[imap_uid($this->mh, $i)] = imap_headerinfo($this->mh, $i);
    }
    return $headers;
  }
  
  function format_address($obj) {
    if (isset($obj->personal)) {
      return imap_rfc822_write_address($obj->mailbox, $obj->host, $obj->personal);
    }
    else {
      return imap_rfc822_write_address($obj->mailbox, $obj->host, '');
    }
  }

  function format_address_list($array, $sep = ", ") {
    $list = array();
    foreach($array as $obj) {
      $list[] = $this->format_address($obj);
    }
    return implode($sep, $list);
  }
  
  private function _decode_body($encoding, $part) {
    switch($encoding) {
      case 3: // Base64
        $strPart = imap_base64($part);
        break;
      case 4: // Quoted printable
        $strPart = imap_qprint($part);
        break;
      case 0: // 7bit
      case 1: // 8bit
      case 2: // Binary
      case 5: // Other
      default:
        break;
    }
    return $part;
  }
  
  private function _mimetype($structure) { 
    $mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"); 
    if($structure->subtype) { 
      return $mime_type[(int) $structure->type] . '/' . $structure->subtype; 
    } 
    return "TEXT/PLAIN"; 
  }

  private function _get_mime_parts($struct, $msg, &$parts, $options = 0, $part_number = false) {
    switch ($struct->type) {
      case 0 :  // TEXT
      case 2 :  // MESSAGE
      case 3 :  // APPLICATION
      case 4 :  // AUDIO
      case 5 :  // IMAGE
      case 6 :  // VIDEO
      case 7 :  // OTHER
        if(!$part_number) { 
          $part_number = "1"; 
        } 
        $data = imap_fetchbody($this->mh, $msg, $options, $part_number); 
        $parts[] = array(
          "DATA" => $this->_decode_body($struct->encoding, $data),
          "MIMETYPE" => $this->_mimetype($struct)
        );
        break;
      case 1 :  // MULTIPART
        $prefix = "";
        while(list($index, $sub_struct) = each($struct->parts)) { 
          if($part_number) { 
            $prefix = $part_number . '.'; 
          } 
          $this->_get_mime_parts($sub_struct, $msg, $parts, $options, $prefix . ($index + 1)); 
        } 
        break;
    } 
  }
  
  function body($msg, $options = 0) {
    $parts = array();
    $struct = imap_fetchstructure($this->mh, $msg, $options);
    $this->_get_mime_parts($struct, $msg, $parts, $options);
    return $parts;
  }
  function delete_email() {
  	//delete email in box
  	imap_delete($this->mh,'1:*');   // to clear out an entire mailbox.
  	imap_expunge($this->mh);
  }
}
?>

 

Careful calling the delete_email method. It will remove the email and will not be existent even in your deleted items on your mail server.

 

then you connect with something like this:

 

$mail = new GetMail("server", "username", "password", "pop3", "");

 

Link to comment
Share on other sites

this is a simple way of using the class:

 

$mail = new GetMail("server", "username", "password", "pop3", "");
$msg = $mail->num_msg();

//get number of messages for the offset
$num = $msg - 1;

if ($msg > 0) {
  $headers = $mail->headers($msg - $num, $num);
  foreach ($headers as $uid=>$header) {
        //date of message
$datemsg = $header->date;
        //subject
        $subjectmsg = $header->subject;
        //who its from
        $from = $header->from;
        //message text

       foreach ($mail->body($uid, FT_UID) as $i=>$part) {
            if ($part['MIMETYPE'] == "TEXT/PLAIN") {
       	$text = $part['DATA'];
            }
       }
       //echo or store your data here
   }
}

 

Hope it works for you. It uses the imap module so you can adjust the class to perform how you like.

 

http://us3.php.net/imap

Link to comment
Share on other sites

for($i=1; $i < $msg-$i+2; $i++) {
  $headers = $mail->headers($i, $msg);
  
  foreach ($headers as $uid=>$header) {
        //date of message
	$datemsg = $header->date;
        //subject
        $subjectmsg = $header->subject;
        //who its from
        $from = $header->from;
        //message text

       foreach ($mail->body($uid, FT_UID) as $i=>$part) {
            if ($part['MIMETYPE'] == "TEXT/PLAIN") {
             $text = $part['DATA'];
            }
       }
   echo "Subject :".$subjectmsg."</br>";
       echo "Body :".$text."<br/><br/>";
  
   }
}

 

im trying to echo all the emails using a for loop.... seems to be going wrong...what am i doing wrong here??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.