IMAP PHP

IMAP is the Internet Message Access Protocol. It was developed in 1986 at Stanford University; however, it has been overshadowed by less sophisticated mail protocols, such as POP (Post Office Protocol). IMAP allows the user to manipulate mail on the server as if it existed locally.

PHP implements IMAP 4, the latest incarnation described in RFmessagesC 1730.

string imap_8bit(string text)
The imap_8bit function converts an 8-bit string into a quote-printable string.

<?
$qtext = imap_8bit($text);
?>

array imap_alerts()
The imap_alerts function returns all the alerts generated by IMAP functions as an array and clears the stack of alerts.

integer imap_append(integer stream, string mailbox, string message, string flags)
The imap_append function appends a message to a mailbox. The stream argument is an integer returned by imap_open. The flags argument is optional.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon","password");
imap_append($mailbox, "INBOX", "This is a message");
imap_close($mailbox);
?>

string imap_base64(string text)
Use imap_base64 to decode base64 text. This routine is part of the IMAP extension; base64_decode is a built-in PHP function that offers the same functionality.

<?
$clear_text = imap_base64($encoded_text);
?>

string imap_binary(string text)
Use imap_binary to convert an 8-bit string into a base64 string.

<?
$base64_text = imap_binary($clear_text);
?>

string imap_body(integer stream, integer message, integer flags)
The imap_body function returns the body of the specified message. The optional flags argument is a bit field that accepts the constants listed. You can use the | operator to combine them.

string imap_body(integer stream, integer message, integer flags)

<?
// get first message and print it
$mailbox = imap_open("{mail.server.com}INBOX", "leon",
"password");
$message = imap_body($mailbox, FT_INTERNAL);
imap_close($mailbox);
print($message);
?>

object imap_bodystruct(integer stream, integer message, integer section)
The imap_bodystruct function returns an object describing the structure of a body section. The object will contain the following properties: bytes, description, disposition, dparameters, encoding, id, ifdescrip-tion, ifdisposition, ifdparameters, ifid, ifparameters, ifsubtype, lines, parameters, subtype, type. The elements such as ifsubtype that begin with if are booleans that signal whether the similarly named elements are present.

object imap_check(integer stream)
The imap_check function returns information about the current mailbox in the form of an object. Table lists the properties of the object. If the connection has timed out, FALSE is returned.

Return-Elements-for-imap_check

string imap_clearflag_full(integer stream, string sequence, string flag, string options)
The imap_clearflag_full function deletes a flag on a sequence of messages. The options argument, if supplied, may be set to ST_UID, which signals that the sequence argument contains UIDs instead of message numbers.

<?
$mailbox = imap_open("{news.server.com/nntp:119}",
"leon", "password");
imap_clearflag_full($mailbox, "12-15", "U", ST_UID);
imap_close($mailbox);
?>

boolean imap_close(integer stream, integer flags)
Use imap_close to close a connection to a mailbox. The stream argument is an integer returned by imap_open. The optional flags argument may be set to CL_EXPUNGE, which will cause the mailbox to be expunged before closing.

imap_create
You may use imap_create as an alias for imap_createmailbox.

boolean imap_createmailbox(integer stream, string mailbox)
Use imap_createmailbox to create a mailbox.

<?
// create a mailbox called PHP List
$mailbox = imap_open("{mail.server.com}INBOX",
"leon","password");
imap_createmailbox($mailbox, "PHP List");
imap_close($mailbox);
?>

boolean imap_delete(integer stream, integer message)
The imap_delete function marks a message for deletion. Use imap_expunge to cause the message to be permanently deleted.

<?
// delete message number 3
$mailbox = imap_open("{mail.server.com}INBOX",
"leon","password");
imap_delete($mailbox, 3);
imap_close($mailbox);
?>

boolean imap_deletemailbox(integer stream, string mailbox)
The imap_deletemailbox function deletes the named mailbox.

<?
// delete a mailbox
$mailbox = imap_open("{mail.server.com}INBOX",
"leon","password");
imap_deletemailbox($mailbox, "PHP List");
imap_close($mailbox);
?>

array imap_errors()
Use imap_errors to get an array of all errors generated by IMAP functions, removing them from an internal stack. You can use imap_last_error to get just the last error.

boolean imap_expunge(integer stream)
Use imap_expunge to remove all messages marked for deletion.

<?
//expunge messages
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
imap_expunge($mailbox);
imap_close($mailbox);
?>

array imap_fetch_overview(integer stream, integer message)
The imap_fetch_overview function returns an array that gives an overview of a message's headers. The array contains the following elements: answered, date, deleted, draft, flagged, from, message_id, msgno, recent,
references, seen, size, subject, uid.

string imap_fetchbody(integer stream, integer message, integer part, integer flags)
The imap_fetchbody function gets a specific part of a message. The body parts are encoded in base64 and must be passed through imap_base64 to be viewed as clear text. The flags argument accepts the flags described.

<?
// get first part of first message
$mailbox=imap_open("{mail.server.com}INBOX", "leon",
"password");
$part1 = imap_fetchbody($mailbox, 1, 1);
imap_close($mailbox);
?>

string imap_fetchheader(integer stream, integer message, integer flags)
Use imap_fetchheader to get the complete RFC 822 header text for a message. The flags argument is a bitfield that takes the following constants: FT_UID, FT_INTERNAL and FT_PREFETCHTEXT. The FT_UID and FT_INTERNAL constants have the same meaning as when used with the imap_body function. The FT_PREFETCHTEXT constant causes the message body to be fetched at the same time.

<?
$mailbox=imap_open("{mail.server.com}INBOX", "leon",
"password");
print(imap_fetchheader($mailbox, 1, FT_PREFETCHTEXT));
imap_close($mailbox);
?>

object imap_fetchstructure(integer stream, integer message)
The imap_fetchstructure returns an object with information about the specified message. Table lists the properties of this object.

object imap_fetchstructure(integer stream, integer message)

<?
// get structure for first message
$mailbox = imap_open("{mail.server.com}INBOX", "leon",
"password");
$structure = imap_fetchstructure($mailbox, 1);
imap_close($mailbox);
?>

imap_fetchtext
You may use imap_fetchtext as an alias for imap_body.

array imap_getmailboxes(integer stream, string reference, string pattern)
The imap_getmailboxes function returns detailed information about mailboxes in the form of an array of objects. The reference argument is an IMAP server in the normal form: "{server:port}". The pattern argument controls which mailboxes are returned. An asterisk (*) matches all mailboxes, and a percentage symbol (%) matches all mailboxes at a particular level. The returned objects contain three properties: name, delimiter, and attributes, a bitfield that may be tested against the constants listed.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
$boxes = imap_getmailboxes($mailbox,
"{mail.server.com:25}", "*");
imap_close($mailbox);
for(list($box) = each($boxes))
{
print("$box->name <BR>n");
}
?>

array imap_getsubscribed(integer stream, string reference, string pattern)
This function returns subscribed mailboxes. The reference and pattern arguments are optional.

array imap_getsubscribed(integer stream, string reference, string pattern)

object imap_header(integer stream, integer message, integer from_length, integer subject_length)
The imap_header function returns an object with properties matching message headers. The from_length and subject_length arguments are optional. These values govern the fetchfrom and fetchsubject properties, respectively.

imap_header-Properties
imap_header-Properties

<?
$mailbox = imap_open("{mail.server.com}INBOX", "leon",
"password");
$header = imap_header($mailbox, 1);
print("Subject: " . $header->subject);
imap_close($mailbox);
?>

imap_headerinfo
The imap_headerinfo function is an alias for imap_header.

array imap_headers(integer stream)
The imap_headers function returns an array of strings, one element per message. Each string summarizes the headers for the message.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
$headers = imap_headers($mailbox);
for($index = 0; $index < count($headers); $index++)
{
print($headers[$index] . "<BR>n");
}
imap_close($mailbox);
?>

string imap_last_error()
Use imap_last_error to get the last error generated by an IMAP function.

imap_list
The imap_list function is an alias to imap_listmailbox.

imap_list_full
The imap_list_full function is an alias to imap_getmailboxes.

array imap_listmailbox(integer stream)
Use imap_listmailbox to get the name of every mailbox in an array.

<?
$mailbox=imap_open("{mail.server.com}INBOX",
"leon","password");
$mailboxes = imap_listmailbox($mailbox);
for($index = 0; $index < count($mailboxes);
$index++)
{
print($mailboxes[$index] . "<BR>n");
}
imap_close($mailbox);
?>

imap_listscan
The imap_listscan function is an alias for imap_scanmailbox.

integer imap_mail(string to, string subject, string message, string headers, string cc, string bcc, string rpath)
The imap_mail function is an alternative to the mail function. The difference is the arguments for specific headers.

string imap_mail_compose(array envelope, array body)
The imap_mail_compose function returns a MIME message given arrays describing the envelope and body. The envelope argument may contain the following elements: bcc, cc, date, from, message_id, reply_to, return_path, to. The body argument may contain the following elements: bytes, contents.data, encoding, id, lines, md5, subtype, type.

boolean imap_mail_copy(integer stream, string list, string mailbox, integer flags)
The imap_mail_copy function copies messages into another mailbox. The list of messages can be a list of messages or a range. The optional flags argument is a bitfield that may be set with CP_UID, which specifies that the list contains UIDs, or CP_MOVE, which instructs the function to delete the original messages after copying. This last functionality may be accomplished with the imap_mail_move function.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
imap_mail_copy($mailbox, "OLD", "17");
imap_close($mailbox);
?>

boolean imap_mail_move(integer stream, string list, string mailbox)
The imap_mail_move function moves messages from the current mailbox to a new mailbox. The list of messages can be a list of messages or a range.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
imap_mail_move($mailbox, "OLD", "17");
imap_close($mailbox);
?>

object imap_mailboxmsginfo(integer stream)
Use imap_mailboxmsginfo to return information about the current mailbox. The object will have the properties listed.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
$info = imap_mailboxmsginfo($mailbox);
print("Driver: " . $info->Driver);
imap_close($mailbox);
?>

integer imap_msgno(integer stream, integer uid)
The imap_msgno function returns the message number based on a UID. To get the UID based on message number, use imap_uid.

Properties-for-imap_mailboxmsginfo

integer imap_num_msg(integer stream)
The imap_num_msg function returns the number of messages in the current mailbox.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
print("Number of Messages: " .
imap_num_recent($mailbox));
imap_close($mailbox);
?>

integer imap_num_recent(integer stream)
The imap_num_recent function returns the number of recent messages in the current mailbox.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
print("Number of Messages: " .
imap_num_msg($mailbox));
imap_close($mailbox);
?>

integer imap_open(string mailbox, string username, string password, integer flags)
Use imap_open to begin a connection to a mail server. The mailbox argument is usually formed by adding a hostname to the beginning of the mailbox. The hostname is enclosed in curly braces. Adding /pop3 causes the imap_open function to connect to a POP server instead of an IMAP server. Adding /nntp allows you to connect to a usenet server. You may also specify a port number, using a colon to separate it from the hostname. A stream identifier is returned. Use this identifier with the IMAP functions that require a stream. The optional flags argument is a bitfield that uses the constants listed.

Constants-Used-by-imap_open

<?
//connect to a normal IMAP server
$mailbox = imap_open("{mail.server.com:143}INBOX",
"leon", "password");
//connect to a POP3 server
$mailbox =
imap_open("{mail.server.com/pop3:110}INBOX", "leon",
"password");
//connect to a NNTP server
$mailbox =
imap_open("{mail.server.com/nntp:119}INBOX", "leon",
"password");
?>

boolean imap_ping(integer stream)
The imap_ping function checks the stream to makes sure it is still alive. If new mail has arrived, it will be detected when this function is called.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
imap_ping($mailbox);
imap_close($mailbox);
?>

integer imap_popen(string mailbox, string username, string password, integer flags)
The imap_popen function opens a persistent connection to an IMAP server. This connection is not closed until the calling process ends, so it may be reused by many page requests. At the time of this writing the code behind this function was unfinished.

string imap_qprint(string text)
The imap_qprint function converts a quote-printable string into an 8-bit string.

<?
$converted = imap_qprint($qstring);
?>

imap_rename
You may use imap_rename as an alias for imap_renamemailbox.

boolean imap_renamemailbox(integer stream, string old_name, string new_name)
The imap_renamemailbox function changes the name of a mailbox.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
imap_renamemailbox($mailbox, "PHP", "PHP List");
imap_close($mailbox);
?>

boolean imap_reopen(integer stream, string username, string password, integer flags)
Use imap_reopen to open a connection that has died. Operation is identical toimap_open.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
// if connection is dead, reopen
if(!imap_ping($mailbox))
{
imap_reopen($mailbox, "leon", "password");
}
imap_close($mailbox);
?>

array imap_rfc822_parse_adrlist(string address, string host)
The imap_rfc_parse_adrlist function parses an email address given a default host and returns an array of objects. Each object has the following properties: mailbox, host, personal, adl. The mailbox property is the name before the @. The host property is the destination machine or domain. The personal property is the name of the recipient. The adl property is the source route, the chain of machines the mail will travel, if the address is specified in that style. As the name of the function suggests, this function implements addresses according to RFC 822.

<?
$address = "Leon Atkinson <[email protected]>,
vicky";
$info = imap_rfc822_parse_adrlist($address,
"clearink.com");
while(list($adr_info) = each($info))
{
print("$adr_info->personal ");
print("$adr_info->mailbox ");
print("$adr_info->host ");
print("$adr_info->adl<BR>n");
}
?>

string imap_rfc822_write_address(string mailbox, string host, string personal_info)
The imap_rfc822_write_address returns an email address. As its name suggests, this function implements addresses according to RFC 822.

<?
print(imap_rfc822_write_address("leon",
"clearink.com", "Leon
Atkinson"));
?>

imap_scan
You may use imap_scan as an alias for imap_scanmailbox.

array imap_scanmailbox(integer stream, string fragment)
The imap_scanmailbox function returns an array of mailbox names that contain the given fragment.

<?
$mailbox = imap_open("{news.server.com/nntp:119}",
"leon", "password");
$name = imap_scanmailbox($mailbox, "alt.");
while(list($match) = each($name))
{
print("$match<BR>n");
}
imap_close($mailbox);
?>

array imap_search(integer stream, string criteria, integer flags)
Use imap_search to get a list of message numbers based on search criteria. The criteria argument is a list of search codes separated by spaces. The codes are listed. Some of them take an argument, which must always be surrounded by double quotes. The optional flags argument may be set to SE_UID to cause UIDs to be returned instead of message numbers.

Imap_search-Criteria-Codes
Imap_search-Criteria-Codes

<?
//get list of unread messages from Leon
$mailbox = imap_open("{mail.server.com:143}INBOX",
"leon", "password");
$msgs = imap_search($mailbox, "UNSEEN FROM
"leon"");
imap_close($mailbox);
?>

string imap_setflag_full(integer stream, string sequence, string flag, string options)
The imap_setflag_full function sets a flag on a sequence of messages. The options argument, if supplied, may be set to ST_UID, which signals that the sequence argument contains UIDs instead of message numbers.

<?
$mailbox = imap_open("{mail.server.com:143}INBOX",
"leon", "password");
imap_setflag_full($mailbox, "12-15", "U", ST_UID);
imap_close($mailbox);
?>

array imap_sort(integer stream, integer criteria, integer reverse, integer options)
Use the imap_sort function to get a sorted list of message numbers based on sort criteria. The criteria argument must be one of the constants defined. If the reverse argument is set to 1, the sort order will be reversed. The options argument is a bitfield that may be set with SE_UID, specifying that UIDs are used, or SE_NOPREFETCH, which will stop messages from being prefetched.

Criteria-Constants-for-imap sort
Criteria-Constants-for-imap_sort

<?
$mailbox = imap_open("{mail.server.com:143}",
"leon", "password");
$list = imap_sort($mailbox, SORTFROM, 0,
SE_NOPREFETCH);
while(list($msg_num) = each($list))
{
print("$msg_num ");
}
imap_close($mailbox);
?>

object imap_status(integer stream, string mailbox, integer options)
The imap_status function returns an object with properties describing the status of a mailbox. The only property guaranteed to exist is flags, which tells you which other properties exist. You choose the properties to generate with the options argument.
Constants to use for options are listed.

imap status-Options

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
$status = imap_status($mailbox, "INBOX",
SA_UNSEEN | SA_MESSAGES);
print("$status->unseen of $status->messages new
messages");
imap_close($mailbox);
?>

boolean imap_subscribe(integer stream, string mailbox)
Use imap_subscribe to subscribe to a mailbox.

<?
$mailbox = imap_open("{mail.server.com}INBOX", "leon",
"password");
imap_subscribe($mailbox, "PHP");
imap_close($mailbox);
?>

integer imap_uid(integer stream, integer message)
The imap_uid function returns the UID for the given message. To get the message number based on UID, use imap_msgno.

boolean imap_undelete(integer stream, integer message)
The imap_undelete function removes the deletion mark on a message.

<?
// delete message number 3, then undelete
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
imap_delete($mailbox, 3);
imap_undelete($mailbox, 3);
imap_close($mailbox);
?>

boolean imap_unsubscribe(integer stream, string mailbox)
Use imap_unsubscribe to unsubscribe to a mailbox.

<?
$mailbox = imap_open("{mail.server.com}INBOX",
"leon", "password");
imap_unsubscribe($mailbox, "PHP");
imap_close($mailbox);
?>

string imap_utf7_decode(string data)
The imap_utf7_decode function takes UTF-7 encoded text and returns plain text.

string imap_utf7_encode(string data)
The imap_utf7_encode function returns UTF-7 encoded text

« Previous Topics
Gettext
Com
Aspell
Next Topics »
Java
Ldap
Semaphores


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics