To do anything with an IMAP account you need to connect to it first. To do this you need to specify some required parameters:
- IMAP is 143 or 993 (secure)
- POP is 110 or 995 (secure)
- SMTP is 25 or 465 (secure)
- NNTP is 119 or 563 (secure)
Flag | Description | Options | Default |
—— | —— | —— | —— |/service=service
| Which service to use | imap, pop3, nntp, smtp | 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 | | disabled |/secure
| do not transmit a plaintext password over the network | | |/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
| certificates from TLS/SSL server | | enabled |/novalidate-cert
| do not validate certificates from TLS/SSL server, needed if server uses self-signed certificates. USE WITH CAUTION | | disabled |/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)
Your connection string will look something like this:
{imap.example.com:993/imap/tls/secure}
Please note that if any of the characters in your connection string is non-ASCII it must be encoded with utf7_encode($string).
To connect to the mailbox, we use the imap_open command which returns a resource value pointing to a stream:
<?php
$mailbox = imap_open("{imap.example.com:993/imap/tls/secure}", "username", "password");
if ($mailbox === false) {
echo "Failed to connect to server";
}