imap_search prompts user to download .php file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

imap_search prompts user to download .php file

Post by batfastad »

Hi everyone

I've recently started experimenting with PHP's imap_* functions

I can connect to our IMAP server over SSL, list folders, count messages... but as soon as I introduce the imap_search function, the user gets prompted to download the PHP file rather than it displaying in the browser.
When I look at the downloaded file, it's empty, 0 bytes.
No PHP error message. When I look in httpd error.log there's nothing that corresponds to that event, nothing for today in fact since it's my test server.

As soon as I take the imap_search function out, the output is displayed in the browser again!

I'm running php 5.2.10 on Apache 2.2.8 on CentOS 5.4 64bit and never had this .php download problem before. Normally when I see that happening it means the file handlers in httpd.conf are messed up but our intranet users have been using this server for about 6 months and never mentioned seeing a .php file download prompt.
I installed the PHP imap extension by doing: yum install php-imap in CentOS, then restarting httpd
I use the centos-test repo as that gives me access to later versions of Apache/PHP/MySQL than the standard CentOS repo.

According to the IMAP section in phpinfo...

Code: Select all

IMAP c-Client Version 2004
SSL Support enabled
Kerberos Support enabled
Should it be a more recent version than 2004?
I noticed that this bug... http://bugs.php.net/bug.php?id=44339 was fixed as of 5.2.5 so that shouldn't affect me. So it appears I probably am using version 2004.

Anyone aware of any bugs/issues that I might be stumbling on?
I had a search through bugs.php.net but couldn't see much IMAP related.

Cheers, B
Last edited by batfastad on Thu Jul 01, 2010 7:32 am, edited 1 time in total.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: imap_search prompts user to download .php file instead o

Post by Jade »

You need to add the file file extension/MIME type to your server.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: imap_search prompts user to download .php file instead o

Post by batfastad »

What file extension? .php?
It already is.
This server's been happily parsing PHP files for about 8 months, and the httpd.conf I've been using has worked on previous servers for 3+ years.

As soon as I add the imap_search function to a script, the user gets prompted to download the file instead of displaying in the browser. All the other imap functions I've used so far appear to work fine, though I've only been experimenting with the IMAP functions over the past few days.

Cheers, B
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: imap_search prompts user to download .php file instead o

Post by Jade »

Sorry I misunderstood the original problem -- what you're saying is that you're not trying to download a br file, you just want to display the search results to the screen.

I wonder if it has something to do with the content headers getting changed after the imap stream is opened. I would make sure the imap stream is closed and set the content headers to text/html before you return the results. Hopefully that helps!
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: imap_search prompts user to download .php file

Post by batfastad »

I'm not even trying to return the results to the browser at this stage. Just process the messages matching that imap_search request.

Tried adding...

Code: Select all

header('Content-Type: text/html;');
But I still get prompted to download the .php file

As soon as I remove the imap_search() function, the problem stops and my script carries on executing!

Any ideas?
Cheers, B
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: imap_search prompts user to download .php file

Post by Jade »

Can you post your code?
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: imap_search prompts user to download .php file

Post by batfastad »

Certainly can, for what it's worth...

Code: Select all

<?php
$imap_conn = imap_open('{server:993/imap/ssl}INBOX/bouncedmessages', 'imapusername', 'imappassword');
echo imap_num_msg($imap_conn);
$folders = imap_listmailbox($imap_conn, '{zimbra.dealer-world.com:993/imap/ssl}', '*');

$result = imap_search($imap_conn, 'SUBJECT "Undelivered Mail Returned"');
foreach($result as $msgno) {
    $headers = imap_fetchheader($imap_conn, $msgno);
    echo $headers;
}

imap_close($imap_conn);
?>
As soon as I remove the imap_search line then it outputs to the browser, rather than prompting to download an empty PHP file.
The imap_num_msg and imap_listmailbox functions work perfectly!

Cheers, B

EDIT: Tried using the

Code: Select all

 BB code tags but the HTML special chars got munged for some reason.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: imap_search prompts user to download .php file

Post by Jade »

Do you have all error reporting turned on? What happens if you run:

Code: Select all

<?php
$imap_conn = imap_open('{server:993/imap/ssl}INBOX/bouncedmessages', 'imapusername', 'imappassword');
echo imap_num_msg($imap_conn);
$folders = imap_listmailbox($imap_conn, '{zimbra.dealer-world.com:993/imap/ssl}', '*');

$result = imap_search($imap_conn, 'SUBJECT "Undelivered Mail Returned"');
print_r($result);

imap_close($imap_conn);
?>
Post Reply