• PHP Video Tutorials

PHP - imap_mail_compose() Function



PHP−IMAP functions helps you to access 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_mail_compose() function accepts two arrays containing header fields and indexed array of bodies as parameters and creates a MIME message.

Syntax

imap_mail_compose($envelope, $body);

Parameters

Sr.No Parameter & Description
1

envelope (Mandatory)

This is an array of headers with the following keys:

remail, return_path, date, from, reply_to, in_reply_to, subject, to, cc, bcc, message_id and custom_headers.

2

body (Mandatory)

This is an array representing the message body with the following keys:

type, encoding, charset, type.parameters, subtype, id, description, disposition.type, disposition, contents.data, lines, bytes and md5.

Return Values

This function returns a string value representing MIME message.

PHP Version

This function was first introduced in PHP Version 4 and works in all the later versions.

Example

Following example demonstrates the usage of the imap_mail_compose() function −

<html>
   <body>
      <?php
         $envelope["from"]= "sender@test.com";
         $envelope["to"]  = "reciever1@test.com";
         $envelope["cc"]  = "reciever2@test.com";

         $mail_part1["type"] = TYPEMULTIPART;
         $mail_part1["subtype"] = "mixed";

         $mail_part2["type"] = TYPETEXT;
         $mail_part2["subtype"] = "plain";
         $mail_part2["description"] = "test_desc";
         $mail_part2["contents.data"] = "sample contents \n\n\n\t";

         $body[1] = $mail_part1;
         $body[2] = $mail_part2;

         print( imap_mail_compose($envelope, $body));
      ?>
   </body>
</html>

Output

This generates the following output −

From: sender@test.com
To: reciever1@test.com
cc: reciever2@test.com
MIME-Version: 1.0
Content-Type: MULTIPART/mixed; BOUNDARY="15319133-10280-1603871611=:4416"

−−15319133-10280−1603871611=:4416
Content-Type: TEXT/plain; CHARSET=US-ASCII
Content−Description: test_desc

sample contents
−−15319133-10280-1603871611=:4416−−
php_function_reference.htm
Advertisements