1

I am trying to read emails using php,i am using below code but its shoeing an error Fatal error: Call to undefined function imap_open() in /testpage2/testmail.php on line 18 can anyone guide me.thanks

<?php
$codes = array("7bit","8bit","binary","base64","quoted-printable","other");
$stt = array("Text","Multipart","Message","Application","Audio","Image","Video","Other");

$pictures = 0;
$html = "";

# Connect to the mail server and grab headers from the mailbox

$mail = imap_open('{mail.xxxxxxxx.com:110/pop3}', 'xxxxxx@xxxxxxxx.com', 'xxxxxxxxx');
$headers = imap_headers($mail);

# loop through each email

for ($n=1; $n<=count($headers); $n++) {
        $html .=  "<h3>".$headers[$n-1]."</h3><br />";

# Read the email structure and decide if it's multipart or not

        $st = imap_fetchstructure($mail, $n);
        $multi = $st->parts;
        $nparts = count($multi);
        if ($nparts == 0) {
                $html .=  "* SINGLE part email<br>";
        } else{
                $html .=  "* MULTI part email<br>";
        }

# look at the main part of the email, and subparts if they're present

        for ($p=0; $p<=$nparts; $p++) {
                $text =imap_fetchbody($mail,$n,$p);
                if ($p ==  0) {
                        $it = $stt[$st->type];
                        $is = ucfirst(strtolower($st->subtype));
                        $ie = $codes[$st->encoding];
                } else {
                        $it = $stt[$multi[$p-1]->type];
                        $is = ucfirst(strtolower($multi[$p-1]->subtype));
                        $ie = $codes[$multi[$p-1]->encoding];
                }

# Report on the mimetype

                $mimetype = "$it/$is";
                $html .=  "<br /><b>Part $p ... ";
                $html .=  "Encoding: $ie for $mimetype</b><br />";

# decode content if it's encoded (more types to add later!)

                if ($ie == "base64") {
                        $realdata = imap_base64($text);
                        }
                if ($ie == "quoted-printable") {
                        $realdata = imap_qprint($text);
                        }

# If it's a .jpg image, save it (more types to add later)

                if ($mimetype == "Image/Jpeg") {
                        $picture++;
                        $fho = fopen("imx/mp$picture.jpg","w");
                        fputs($fho,$realdata);
                        fclose($fho);
                        # And put the image in the report, limited in size
                        $html .= "<img src=/demo/imx/mp$picture.jpg width=150><br />";
                }

# Add the start of the text to the message

                $shorttext = substr($text,0,800);
                if (strlen($text) > 800) $horttext .= " ...\n";
                $html .=  nl2br(htmlspecialchars($shorttext))."<br>";
        }
}

# report results ...

?>
<html>
<head>
<title>Reading a Mailbox including multipart emails from within PHP</title>
</head>
<body>
<h1>Mailbox Summary ....</h1>
<?= $html ?>
</body>
</html>
arok
  • 1,795
  • 7
  • 27
  • 56

6 Answers6

2

You need to enable IMAP in PHP before you can use it. If you do a phpinfo() you can confirm whether it is installed or not.

John Conde
  • 217,595
  • 99
  • 455
  • 496
2

If you are using XAMPP enable the php_imap.dll extension go to your XAMPP/php change in the php.ini file

;extension=php_imap.dll

TO

 extension=php_imap.dll
Ajit Singh
  • 1,132
  • 1
  • 13
  • 24
1

As others said enable or install the imap extenstion (e.g. php5-imap in ubuntu/debian). Btw. take a look at Fetch, it's a great OOP orientated library for handling with IMAP.

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
0

You don't have the IMAP module for PHP installed on your server. install php5-imap.

Ref: http://www.php.net/manual/en/imap.setup.php

Krish R
  • 22,583
  • 7
  • 50
  • 59
0

check weather imap is enabled in your server , if not enabled means enable it . if imap extension not installed means install and enable it,check this link http://www.php.net/manual/en/imap.setup.php

Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30
0

if you have php 7+ the update your php.ini file

uncomment

;extension=imap

To

extension=imap

if you cant find extension=imap then add it manually to your php.ini to section where other extensions are defined by simply search for ;extension

and finally restart Apache

user889030
  • 4,353
  • 3
  • 48
  • 51