2

I'm setting up an email 'proxy-like' where notification emails are sent to the user that he has subscribed to, but he can reply to the email notification@site.com which should contain his reply. On the server-side I want to be able to read his email and add his reply to the database - the email is stored in the notification user inbox which is great, now to be able to read it with PHP.

So, I've been looking around for solutions for this, many solutions were IMAP so I thought I'd give a try but I cannot seem to figure my connection my server to open the inbox. I've adjusted to different ports, but none of them are able to connect.

imap_open("{localhost/imap:143}", "notification", "password");

I get errors like this:

Warning: imap_open(): Couldn't open stream {localhost/imap:143} in /var/www/site.com/www/mail.php on line 8
bool(false)

Notice: Unknown: Connection failed to localhost,143: Connection refused (errflg=1) in Unknown on line 0

Notice: Unknown: Connection failed to localhost,143: Connection refused (errflg=1) in Unknown on line 0

Notice: Unknown: Connection failed to localhost,143: Connection refused (errflg=1) in Unknown on line 0

Notice: Unknown: Connection failed to localhost,143: Connection refused (errflg=2) in Unknown on line 0

I don't understand, obviously this is my first time trying this but what is it that I'm clearly doing wrong with the connection?

EDIT: I started courier-imap now I get this:

Notice: Unknown: [CLOSED] IMAP connection broken (server response) (errflg=1) in Unknown on line 0

Notice: Unknown: [ALERT] Fatal error: No such file or directory: No such file or directory in Unknown on line 0
MacMac
  • 34,294
  • 55
  • 151
  • 222
  • A few things come to mind. One of them has to do with your firewall. If port 143 is blocked, that would cause the connection to fail. – Matt Aug 06 '12 at 14:53
  • So do you know any common ports that can connect to the mail inboxes? – MacMac Aug 06 '12 at 14:54
  • It all depends on how your mailserver is set up. All I'm suggesting is that you have to open up port 143 in order for this to work (if it's blocked, that is). – Matt Aug 06 '12 at 14:56
  • Tried telnet-ing to the mail server? http://bobpeers.com/technical/telnet_imap – Waygood Aug 06 '12 at 15:00
  • I just opened the port `143` by doing this `iptables -A INPUT -p tcp --dport 143 -j ACCEPT` but I still cannot connect to it nor telnet. – MacMac Aug 06 '12 at 15:07
  • 1
    @BurningtheCodeigniter Are you absolutely 115% certain that a) the IMAP service is running and b) it's listening on TCP:143? I would say it's highly unlikely a firewall is getting in the way of loopback traffic, seems far more likely that there's simply nothing to connect to. – DaveRandom Aug 06 '12 at 15:24
  • Well I installed imap via `apt-get install php5-imap`, is there a dependency for this? – MacMac Aug 06 '12 at 15:39
  • @BurningtheCodeigniter I'm not talking about the PHP extension - that's the client. I'm talking about the actual IMAP server that your code thinks is listening on `localhost:143` – DaveRandom Aug 06 '12 at 15:54
  • Oh, `courier-imap` wasn't running, it is now running - this time I get `Notice: Unknown: [CLOSED] IMAP connection broken (server response) (errflg=1) in Unknown on line 0 Notice: Unknown: [ALERT] Fatal error: No such file or directory: No such file or directory in Unknown on line 0` – MacMac Aug 06 '12 at 16:00
  • @BurningtheCodeigniter Can you now `telnet localhost 143` and hit an IMAP server? – DaveRandom Aug 06 '12 at 16:07
  • Yup, I can from my side and the server side. – MacMac Aug 06 '12 at 16:09
  • @BurningtheCodeigniter Take a look at [this](http://forums.cpanel.net/f5/error-when-trying-add-imap-php-48095.html#post237487). The error message you report seems to be fairly common - that's an old thread but it seems to reflect what you are trying to do and suggests a possible fix which other users say worked for them. – DaveRandom Aug 06 '12 at 16:10
  • I'm on Ubuntu 11.10 btw. – MacMac Aug 06 '12 at 16:13
  • @BurningtheCodeigniter Did you try installing Gamin as the linked forum thread suggests? – DaveRandom Aug 06 '12 at 22:45
  • Well I'm on Ubuntu not CentOS, I did `apt-get install gamin` but that didn't help whatsoever, maybe it's a different package? – MacMac Aug 06 '12 at 23:24
  • I've been researching trying to get a solution, I found one http://ubuntuforums.org/showthread.php?t=1970712 but unfortunately it didn't help. I still get the same errors as before. – MacMac Aug 07 '12 at 15:32
  • @DaveRandom I love how you're ignoring this... – MacMac Aug 08 '12 at 11:39
  • @BurningtheCodeigniter Sorry I didn't get notified of your last comments because you didn't @-tag me in it. I have to say I've sort of run out of ideas now - it seems this is a very common error message when using the `imap_*` functions, but not one I've ever come across. I would start a bounty on it but it's not old enough yet, It think it can be done after 48hrs which is in about 2hrs. – DaveRandom Aug 08 '12 at 11:45

2 Answers2

3

You are asking for basic debugging here, so I try, maybe this machtes

Have you tried to specify a mailbox? And btw you should actually use the right syntax, it is host:port/protocol whereas imap is the default protocol and you can leave it out:

imap_open("{localhost:143}INBOX", "notification", "password");
            ^^^^^^^^^^^^^ ^^^^^

Please compare with the many examples and notes on imap_openDocs.

Also as this is debugging, try:

var_dump(imap_errors(), imap_alerts());

Happy debugging.

hakre
  • 193,403
  • 52
  • 435
  • 836
1

I had exactly the same problem on my Ubuntu 11.04 system, and I've solved by creating the "Maildir" folders in my home directory (as described here) with the following commands:

cd ~
maildirmake Maildir
maildirmake Maildir/.Drafts
maildirmake Maildir/.Sent
maildirmake Maildir/.Trash
maildirmake Maildir/.Templates
chmod -R 700 Maildir

First of all, you should check for errors in the log file "/var/log/syslog" (for example by executing the command sudo tail /var/log/syslog).
In my log I had noticed the following error: imapd: chdir Maildir: No such file or directory.
So I've checked the configuration file "/etc/courier/imapd" and I saw that the MAILDIRPATH option was set to "Maildir":

#
# MAILDIRPATH - directory name of the maildir directory.
#
MAILDIRPATH=Maildir

Then I've created the "Maildir" folders as described above, and the "imap_open()" function finally worked.

  • When I did `sudo tail /var/log/syslog` on the root account, I get login errors in the file, but the user `notification` and the password is correct. – MacMac Aug 14 '12 at 22:21
  • Maybe the error is before the last 10 lines of the file "/var/log/syslog", so with the "tail" command you can't find the "imapd" error. Try this command: `sudo grep imapd /var/log/syslog`. However, did you check the MAILDIRPATH setting in the "/etc/courier/imapd" file? If it's set to "Maildir", did you check that the "/home/notification/Maildir" folder exists? If not, can you create it with the commands shown in my answer? Obviously, instead of `cd ~` you must do `cd /home/notification`. –  Aug 15 '12 at 09:05
  • There are no errors included in the syslog of `imapd`, also the `/home/notification/Maildir` exists. – MacMac Aug 15 '12 at 13:30
  • `Aug 15 16:47:34 server imapd: Connection, ip=[::ffff:xx.xx.xx.xx] Aug 15 16:47:34 server imapd: LOGIN FAILED, user=notification@mysite.com, ip=[::ffff:xx.xx.xx.xx] Aug 15 16:47:49 imapd: last message repeated 2 times Aug 15 16:47:49 server imapd: LOGOUT, ip=[::ffff:xx.xx.xx.xx], rcvd=164, sent=385` – MacMac Aug 15 '12 at 14:50
  • I replaced the server IP with `xx.xx.xx.xx`. – MacMac Aug 15 '12 at 14:52
  • Try setting `DEBUG_LOGIN=2` in the file "/etc/courier/authdaemonrc", then restart the courier-imap service with the command `sudo service courier-imap restart`. –  Aug 15 '12 at 15:09
  • have a look at [this post](http://www.courier-mta.org/authlib/README.authdebug.html) –  Aug 15 '12 at 16:26
  • Aha, I get this now `Aug 15 18:37:00 server authdaemond: authpam: username 'notification@mysite.com' not found in password file` – MacMac Aug 15 '12 at 16:38
  • Try to remove the domain "@mysite.com" from the username parameter. –  Aug 15 '12 at 18:54
  • Otherwise, read the section "Configuring authdaemond to authenticate virtual users" on [this page](http://www.su3analytics.com/SU3Analytics/docs/mailserver/chapter8) –  Aug 15 '12 at 19:10
  • YAY! That worked! I cannot seem to have any emails displayed, I used **Abid Hussain's** PHP class, using `->get(1)` just shows an empty array, it also says there are no emails but if I look in the `notification` inbox, I got 2 in there (from `/var/spool/mail/notification`). – MacMac Aug 15 '12 at 19:48
  • So, now the problem is that you can connect, but you cannot read the messages, right? Can we eventually chat to avoid further comments? –  Aug 15 '12 at 20:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15391/discussion-between-burning-the-codeigniter-and-enzino) – MacMac Aug 15 '12 at 20:00
  • Accepted solution from discussion, basically change the postfix home_mailbox to `Maildir/`. Bounty awarded. – MacMac Aug 15 '12 at 20:23