
- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Environment Setup
- PHP - Syntax Overview
- PHP - Variable Types
- PHP - Constants
- PHP - Operator Types
- PHP - Decision Making
- PHP - Loop Types
- PHP - Arrays
- PHP - Strings
- PHP - Web Concepts
- PHP - GET & POST
- PHP - File Inclusion
- PHP - Files & I/O
- PHP - Functions
- PHP - Cookies
- PHP - Sessions
- PHP - Sending Emails
- PHP - File Uploading
- PHP - Coding Standard
- Advanced PHP
- PHP - Predefined Variables
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Bugs Debugging
- PHP - Date & Time
- PHP & MySQL
- PHP & AJAX
- PHP & XML
- PHP - Object Oriented
- PHP - For C Developers
- PHP - For PERL Developers
- PHP Form Examples
- PHP - Form Introduction
- PHP - Validation Example
- PHP - Complete Form
- PHP login Examples
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP AJAX Examples
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML Example
- PHP - XML Introduction
- PHP - Simple XML
- PHP - Simple XML GET
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Frame Works
- PHP - Frame Works
- PHP - Core PHP vs Frame Works
- PHP Design Patterns
- PHP - Design Patterns
- PHP Function Reference
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Useful Resources
- PHP - Discussion
PHP - imap_open() Function
PHP−IMAP functions helps you to access an email accounts, IMAP stands for Internet Mail Access Protocol using these functions you can also work with NNTP, POP3 protocols and local mailbox access methods.
The imap_open() function accepts three string values representing the mailbox name/URL, user name and, password as parameters and opens stream to the specified mailbox.
Syntax
imap_open ($mailbox, $username, $password [$options, $n_retries, $params);
Parameters
Sr.No | Parameter & Description |
---|---|
1 |
mailbox(Mandatory) This is a string value representing the name/URL of the mailbox. It contains the server name, mailbox path. |
2 |
username(Mandatory) This is a string value representing the user name. |
3 |
password(Mandatory) This is a string value representing the password. |
4 |
options (Optional) This is an integer value representing the optional parameter which can be one or more of the following −
|
5 |
retries (Optional) This is an integer value representing the maximum number of attempts. |
6 |
params (Optional) This is an array value representing the connection parameters. |
Return Values
This function returns an IMAP stream object in case of success and a Boolean value FALSE in case of failure.
PHP Version
This function was first introduced in PHP Version 4 and works in all the later versions.
Example
Following is an php program tries to establish a connection with a particular Gmail account imap_open() −
<html> <body> <?php $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX"; $id = "tutorialspoint.test@gmail.com"; $pwd = "cohondob_123"; $mailbox = imap_open($url, $id, $pwd); if($mailbox){ print("Connection established...."); } else { print("Connection failed"); } ?> </body> </html>
Output
The above program generates the following output −
Connection established....
Example
Following is another example of this function in this we have established connection to a particular mail box and retrieved the contents of the message in it.
<html> <body> <?php //Establishing connection $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX"; $id = "tutorialspoint.test@gmail.com"; $pwd = "cohondob_123"; $imap = imap_open($url, $id, $pwd); print("Connection established...."."<br>"); //Searching emails $emailData = imap_search($imap, ''); if (! empty($emailData)) { foreach ($emailData as $msg) { $msg = imap_fetchbody($imap, $msg, "1"); print(quoted_printable_decode($msg)."<br>"); } } //Closing the connection imap_close($imap); ?> </body> </html>
Output
The above program generates the following output −
Connection established.... This is a test mail #1. --0000000000001bf26805af905277 Content-Type: text/plain; charset="UTF-8" test mail #2 --0000000000001bf26805af905277 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable test mail #2 --0000000000001bf26805af905277-- test mail #3 test mail #4
Example
Following is the example of this function with optional parameters.
<html> <body> <?php //Establishing the connection $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX"; $id = "tutorialspoint.test@gmail.com"; $pwd = "cohondob_123"; //Optional parameters $options = OP_READONLY; $retries = 10; $mailbox = imap_open($url, $id, $pwd, $options, $retries); if($mailbox){ print("Connection established...."); } else { print("Connection failed"); } ?> </body> </html>
The above program generates the following output −
Connection established....