2
votes

A user over at w3schools forums assisted me with some code on using IMAP functions to check my mail inbox on a private server and do what i like with it, i created my own set of functions for posting the e-mail content to a MySQL table.

Could somebody help me find a solution to how can i open the e-mail inbox, check for e-mails in the inbox (There will be only one there because previous emails will be automatically deleted. Define the open e-mail message as $open_email_msg Allow me to initiate my set of commands for posting the e-mail to a MySQL table, then delete the e-mail and close the inbox?

This is the code the person assisted me with:

<?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
2

2 Answers

1
votes

Try this code to read emails.

$username="[email protected]";
$password="yourPassword123!";
$hostname="{imap.hostinger.com:993/imap/ssl}INBOX";

$imap=imap_open($hostname,$username,$password) or die('Cannot connect: '.imap_last_error());
$message_count = imap_num_msg($imap);
echo "<b>$message_count messages</b><br>";

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
votes

I believe this link will help you as I used this myself and this is properly working for me.

There you can register and download the code, using it is simple.

Or what you can do if you want to get the information of header only is:

$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>";
}