Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Connecting to an imap server using imap_open

David Carr

IMAP PHP & MySQL Tutorials

    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("{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.

     //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:

      //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.

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.