What role does the port number (e.g., 143) play when using imap_open in PHP?

The port number in imap_open function in PHP specifies the port to connect to the IMAP server. By default, IMAP uses port 143 for non-secure connections and port 993 for secure connections (using SSL). If you are facing connection issues with imap_open, specifying the correct port number can help establish a successful connection to the IMAP server.

$mailbox = '{mail.example.com:993/imap/ssl}INBOX';
$username = 'your_username';
$password = 'your_password';

$connection = imap_open($mailbox, $username, $password);

if (!$connection) {
    die('Connection failed: ' . imap_last_error());
} else {
    echo 'Connection successful!';
    
    // Work with the IMAP connection
    
    imap_close($connection);
}