Connecting to an imap server using imap_open

Connecting to an imap server using imap_open

May 6, 2011·

3 min read

This first tutorial will show you how to use imap_open to connect to a imap mail server.

To connect to an IMAP server using imap_open requires 3 parameters the host = either an ip address of the domain name, username = the email address and mailbox login name and finally the account password.

imap_open("{}", "","password");

The host includes the domain name and the port to be used in the format of, there are a number of optional flags that can be used look at the table below:

Flag Description
/service=service mailbox access service, default is "imap"
/user=user remote user name for login on the server
/authuser=user remote authentication user; if specified this is the user name whose password is used (e.g. administrator)
/anonymous remote access as anonymous user
/debug record protocol telemetry in application's debug log
/secure do not transmit a plaintext password over the network
/imap, /imap2, /imap2bis, /imap4, /imap4rev1 equivalent to /service=imap
/pop3 equivalent to /service=pop3
/nntp equivalent to /service=nntp
/norsh do not use rsh or ssh to establish a preauthenticated IMAP session
/ssl use the Secure Socket Layer to encrypt the session
/validate-cert validate certificates from TLS/SSL server (this is the default behavior)
/novalidate-cert do not validate certificates from TLS/SSL server, needed if server uses self-signed certificates
/tls force use of start-TLS to encrypt the session, and reject connection to servers that do not support it
/notls do not do start-TLS to encrypt the session, even with servers that support it
/readonly request read-only mailbox open (IMAP only; ignored on NNTP, and an error with SMTP and POP3)

Depending on your setup you'll need to use a different combination of flags in the host sections of imap_open notls/norsh/novalidate-cert works well for most custom domain names, I'll include a list of hosts strings that I've successfully used to connect to external imap servers such as Hotmail, Gmail and AOL.

 //open connection
   $mbox = imap_open("{}", "","password")or die(imap_last_error())or die("can't connect: ".imap_last_error());

I make use of imap_last_error() which returns the last error from the requested server. if failed to login you'll get an error similar to this:

Warning: imap_open() [function.imap-open]: Couldn't open stream {} in /path/to/file.php  on line 3 Can not authenticate to IMAP server: Authentication failed.

List of host strings:

Provider Host String
Karoo {}
AOL {}
AIM {}
Lycos {} {}
Hotmail/MSN/Live {}
Googlemail/Gmail {}

For example if I wanted to connect to a Gmail account:

  //open connection
   $mbox = imap_open("{}", "","password")or die(imap_last_error())or die("can't connect: ".imap_last_error());

When imap_open() is ran you'll get an error if the connection fails otherwise you won't get any feedback which indicates a successful connection.

Did you find this article valuable?

Support David Carr by becoming a sponsor. Any amount is appreciated!