Avatar of Ron1959
Ron1959
 asked on

PHP to retreive mail from a pop3 server - what is best practice?

I need for a PHP script to retrieve email from a pop3 server.

I have tried
$connection = imap_open('{mail.domain.com:110/pop3}', $user, $pass);

Open in new window


But that results in:  imap_open() [function.imap-open]: Couldn't open stream

The credentials check out fine in an email client.  Is there a better way to retrieve email?  If not, do you have any idea what the cause of the error could be?

phpinfo() indicates that  imap ssl support is enabled, so I assume imap is, too.

PHP Version 5.2.8 -- part of an XAMPP installation.

Thanks!

- Ron
PHP

Avatar of undefined
Last Comment
Ron1959

8/22/2022 - Mon
Loganathan Natarajan

can you add these 2 lines below the connect, and check,

imap_errors();
imap_alerts();
Ron1959

ASKER
Thanks for the response.

I did add imap_errors();  and imap_alerts(); but the result did not change.  I am receiving the same error message.
Loganathan Natarajan

Probably some  blocking may be there like firewall or permission etc., You need to check it.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ron1959

ASKER
Two questions:
1) If I'm getting the imap error, does that mean, for sure, that imap is installed?

2) I am using the exact same credentials I successfully use to access the email account with Thunderbird, why would a firewall or permissions affect imap?


BTW - The php script is running on the same server as our pop server.

Thanks!
Ray Paseur

This may not directly answer questions about why PHP is not working with imap, but it might offer a design idea.  Instead of retrieving the email messages, you might consider using an email PIPE script to process the messages as they arrive.  The PIPE script is started when the email message comes to a specific in-box.  It finds the entire message including all headers and attachments in the STDIN.  A PIPE can log the messages to a data base, trigger other automated activities, or even re-send the email to a human client.  In my experience it was easier to use the PIPE than to try to connect and read messages after the fact.
Ron1959

ASKER
Ray,

I think you have the right approach, but our email server is a Windows box and we are running MailServer.NET.  I'm not sure it will have the same capabilities as something like sendmail.

You have piqued my interest, though, and I will ask the MailServer.NET developer about it's capabilities.

Thanks!

- Ron
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

Good luck with it.  I do not know enough about .NET to even spell it, but I can show you a teaching example of an email pipe that runs on my linux server.  The code starts around line 30; the first part is just a set of comments that describe how I got it to work.

#!/usr/bin/php -q
<?php // /email_pipe/index.php


// THIS IS AN EMAIL PIPE SCRIPT.
// THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION


// --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
// THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
// CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
// SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
// 1...5...10...15...20...25...
// |/home/{account}/email_pipe/index.php


// --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
// THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
// YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX


// --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
// 1...5...10...15...20...25...
// #!/usr/bin/php -q
// <?php ... PROGRAM CODE FOLLOWS


error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = NULL;

// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin " . PHP_EOL;
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('/ +/', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);
ob_end_clean();

Open in new window

ASKER CERTIFIED SOLUTION
Ron1959

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ron1959

ASKER
I asked a similar question and someone led me to http://us2.php.net/manual/en/function.imap-savebody.php, which is better than the other manual page for imap.