no

How to Read and Update Your Mailbox Using Php

There are 2 ways I use 2 read email from my mailbox. Let the code explain: 1.) Usually use for gmail. /* connect to gmail */ $hostname = ...

There are 2 ways I use 2 read email from my mailbox. Let the code explain:
1.) Usually use for gmail.
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'xxx@gmail.com';
$password = 'xxx';

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

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
echo "Messages count: " + count($emails);
if($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,1);
  
  $output .= '<br />Subject: '. $overview[0]->subject.'<br />';
  $output .= 'From: '. $overview[0]->from.'<br />';
  $output .= 'To: '. $overview[0]->to.'<br />';
  $output .= 'Date: '. $overview[0]->date.'<br />';
  
  /* output the email body */
  $output.= '<div>Message: '.$message.'</div>';
 }
 
 echo $output;
} 

/* close the connection */
imap_close($inbox);
2.) Sometimes I'm having a problem reading the message part in the first method, so I searched for a new way (usually for POP protocol). And this is how, I got it somewhere:
function get_mime_type(&$structure) {
 $primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
 
 if($structure->subtype) {
  return $primary_mime_type[(int) $structure->type] . '/' .$structure->subtype;
  }
 return "TEXT/PLAIN";
}

function get_part($stream, $msg_number, $mime_type, $structure = false,$part_number = false) {
 if(!$structure) {
  $structure = imap_fetchstructure($stream, $msg_number);
 }
 if($structure) {  
  if($mime_type == get_mime_type($structure)) {
   if(!$part_number) {
    $part_number = "1";
      }
     $text = imap_fetchbody($stream, $msg_number, $part_number);
   
     if($structure->encoding == 3) {
    return imap_base64($text);
      } else if($structure->encoding == 4) {
    return imap_qprint($text);
      } else {
       return $text;
      }
   }
  if(!isset($prefix)) {
   $prefix = '';
  }
 if($structure->type == 1) /* multipart */  {
  while(list($index, $sub_structure) = each($structure->parts)) {
   if($part_number) {
    $prefix = $part_number . '.';
      }
     
   $data = get_part($stream, $msg_number, $mime_type, $sub_structure,$prefix . ($index+1));
   if($data) {
       return $data;
      }
     }
   }
 }
return false;
}

$hostname = '{mail.xxx.xxx/pop:110}INBOX';
$username = 'xxx@xxx.xx';
$password = 'xxx';

/* open imap connection */
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to smschannel4: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* 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) {
  /* get information specific to this email */
  $overview = imap_fetch_overview($inbox,$email_number,0);
  $message = get_part($inbox, $email_number, 'TEXT/PLAIN');

  $subject = transformstr($overview[0]->subject);
  $from = ereg_replace("\"","",$overview[0]->from);
  $from = transformstr(imap_utf8($from));
  $to = ereg_replace("\"","",$overview[0]->to);
  $to = transformstr(imap_utf8($to));
  $message = clean($message);  

  //how you would delete from the email server
  //imap_delete($inbox, $email_number);
 }
}
imap_expunge($inbox);
/* close connections */
imap_close($inbox);

echo count($emails) . " message/s processed.";

Related

php 3200899730288995699

Post a Comment Default Comments

item