Connecting to an imap server using imap_open

php allows you to do a lot of things including reading your email! This IMAP series will show you how to use the built in imap functions that php has to offer. The first is imap_open

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.

<?php
imap_open("{mail.example.com:143/notls/norsh/novalidate-cert}", "sample@example.com","password");
?>

The host includes the domain name and the port to be used in the format of mail.domain.com:143, 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.

<?php
   //open connection
   $mbox = imap_open("{mail.example.com:143/notls/norsh/novalidate-cert}", "sample@example.com","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 {mail.example.com:143/notls/norsh/novalidate-cert} in /path/to/file.php on line 3 Can not authenticate to IMAP server: Authentication failed.

List of host strings:

Provider Host String
Karoo {pop.karoo.co.uk:110/pop3/novalidate-cert}
AOL {imap.aol.com:143/imap/novalidate-cert}
AIM {imap.aol.com:143/imap/novalidate-cert}
Lycos {mail.lycos.co.uk:143/pop3/novalidate-cert}
Yahoo.com {mail.yahoo.com:110/pop3/novalidate-cert}
Hotmail/MSN/Live {pop3.live.com:995/pop3/ssl/novalidate-cert}
Googlemail/Gmail {imap.gmail.com:993/imap/ssl/novalidate-cert}

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

<?php
   //open connection
   $mbox = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert}", "sample@gmail.com","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.

Author
Dave

About the Author

Dave has written 45 articles on Developers Tool Kit.

Visit this author's website   ·   View more posts by Dave

Sharing is caring.
  • Subscribe to our feed
  • Share this post on Delicious
  • StumbleUpon this post
  • Share this post on Digg
  • Tweet about this post
  • Share this post on Mixx
  • Share this post on Technorati
  • Share this post on Facebook
  • Share this post on NewsVine
  • Share this post on Reddit
  • Share this post on Google
  • Share this post on LinkedIn

Discussion

One response to "Connecting to an imap server using imap_open"

Leave a Comment