Connecting to a mailbox
suggest changeTo do anything with an IMAP account you need to connect to it first. To do this you need to specify some required parameters:
- The server name or IP address of the mail server
- The port you wish to connect on
- IMAP is 143 or 993 (secure)
- POP is 110 or 995 (secure)
- SMTP is 25 or 465 (secure)
- NNTP is 119 or 563 (secure)
- Connection flags (see below)
| Flag | Description | Options | Default |
|---|---|---|---|
/service=service |
Which service to use | imap, pop3, nntp, smtp | imap |
/user=user |
Remote user name for login | ||
/authuser=user |
User whose password is used, if different from /user |
||
/anonymous |
Log in as the anonymous user | ||
/debug |
Record protocol telemetry | disabled | |
/secure |
Do not send a plaintext password | ||
/norsh |
Do not use rsh or ssh for a preauthenticated session | ||
/ssl |
Use implicit TLS (typical for port 993) | ||
/validate-cert |
Validate the server certificate | enabled | |
/novalidate-cert |
Do not validate the certificate. Needed for some self-signed certificates. Use with caution. | disabled | |
/tls |
Require STARTTLS and reject servers that do not support it (typical for port 143) | ||
/notls |
Do not use STARTTLS | ||
/readonly |
Open the mailbox read-only (IMAP only; ignored for NNTP; an error for SMTP and POP3) |
Your connection string will look something like this:
{imap.example.com:993/imap/ssl}
If the mailbox name contains non-ASCII characters, encode that name with imap_utf7_encode(). There is no utf7_encode() function.
Port 993 is implicit TLS, so the flag is /ssl. /tls means STARTTLS and is normally used with port 143. imap_open() returns false on failure. Through PHP 8.0 a success value is a resource; PHP 8.1 and later return an IMAP\Connection object.
<?php
$mailbox = imap_open("{imap.example.com:993/imap/ssl}", "username", "password");
if ($mailbox === false) {
echo "Failed to connect to server";
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents