PHP 5.6.24 is released


(PHP 4, PHP 5, PHP 7)

imap_appendAñadir un mensaje de cadena a un buzón especificado


bool imap_append ( resource $imap_stream , string $mailbox , string $message [, string $options = NULL [, string $internal_date = NULL ]] )

Añade un mensaje de cadena dado por message al buzón especificado por mailbox.



IMAP stream devuelto por imap_open().


El nombre del buzón, veáse imap_open() para más información


El mensaje a añadir, como cadena

Cuando se toma el servidor Cyrus IMAP, se debe usar "\r\n" como terminador de final de línea en lugar de "\n", o la operación fallará


Si se proporciona, options también serán escritas al mailbox


Si este parámetro se establece, establecerá INTERNALDATE en el mensaje añadido. El parámetro debería ser una cadena de fecha conforme a las especificaciones rfc2060 para un valor date_time.

Valores devueltos

Devuelve TRUE en caso de éxito o FALSE en caso de error.

Historial de cambios

Versión Descripción
5.3.2 Se añadió el soporte de INTERNALDATE para imap_append.


Ejemplo #1 Ejemplo de imap_append()


$check imap_check($stream);
"Msg Count before append: "$check->Nmsgs "\n";

"Subject: test\r\n"
"this is a test message, please ignore\r\n"

$check imap_check($stream);
"Msg Count after append : "$check->Nmsgs "\n";


add a note add a note

User Contributed Notes 14 notes

rixsta at hotmail dot com
3 years ago

As we have been struggling with this for some time I wanted to share how we got imap_append working properly with all MIME parts including attachments.  If you are sending email and also wish to append the sent message to the Sent Items folder, I cannot think of an easier way to do this, as follows:

1) Use SwiftMailer to send the message via PHP.
$message = Swift_Message::newInstance("Subject goes here");
(then add from, to, body, attachments etc)
$result = $mailer->send($message);

2) When you construct the message in step 1) above save it to a variable as follows:

$msg = $message->toString(); (this creates the full MIME message required for imap_append()!!  After this you can call imap_append like this:


I hope this helps the readers, and prevents saves people from doing what we started doing - hand crafting the MIME messages :-0
bill at dyndns dot org
4 years ago
Here is how I used this function with an attachment. I used examples from everyone else but made it easier and cleaner to change.



if (
$mbox=imap_open( $authhost, $user, $pass))
$dmy=date("d-M-Y H:i:s");
$attachment = chunk_split(base64_encode($filestring));
$boundary = "------=".md5(uniqid(rand()));
$msg = ("From: Somebody\r\n"
. "To:\r\n"
. "Date: $dmy\r\n"
. "Subject: This is the subject\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n"
. "\r\n\r\n"
. "--$boundary\r\n"
. "Content-Type: text/html;\r\n\tcharset=\"ISO-8859-1\"\r\n"
. "Content-Transfer-Encoding: 8bit \r\n"
. "\r\n\r\n"
. "Hello this is a test\r\n"
. "\r\n\r\n"
. "--$boundary\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"$filename\"\r\n"
. "\r\n" . $attachment . "\r\n"
. "\r\n\r\n\r\n"
. "--$boundary--\r\n\r\n");
imap_append($mbox,$authhost,$msg, "\\Draft");



Hope this helps someone.
jille at DIESPAMMERShexon dot cx
9 years ago
The last argument, $options, are flags like for use with imap_setflag_full.
It took a while before I found out
kaminski at istori dot com
5 years ago
The date format string to use when creating $internal_date is 'd-M-Y H:i:s O'.
1 year ago
You can use PHPMailer ( ) with imap.

// after creating content of mail you have to run preSend() - part of send() method
// and you can get whole raw message with getSentMIMEMessage() method
imap_append($imap, $mailserver.'INBOX.Sent',$mail->getSentMIMEMessage(), "\\Seen");
svicentemolina at gmail dot com
9 years ago
I have used this function to copy all the emails of one account from one server to another. The problem was that this function don't copy the original receiving date for each message.
To add a fifth field to provide the date, I have made some changes at some php source files following the steps described in and it has worked fine.
The correct date format is the returned by the function mail_date in c-client/mail.c source file, for instance: "17-Jan-2007 10:00:01 +0100"
13 years ago
The parameter description is misleading.  You can pass a string of flags such as '\Seen' [see imap_setflag_full()] as the last argument.  In the other imap functions, 'options' seems to usually refer to a bitmask, not message flags.
michel dot jansens at ulb dot ac dot be
13 years ago
I use imap_append() to decode message/rfc822 attachments type(embedded emails):

$attachment = imap_fetchbody($mbox,$mailuid,$atpos,FT_UID);
$attachment = imap_base64($attachment);
$res =  imap_append($mbox,mboxspec("INBOX"),$attachment);
//the embedded email is now a normal mail in my INBOX
bluebiru78 at hotmail dot com
14 years ago
i used imap_append to copy any composed message into INBOX.Sent folder..

$app = imap_append($stream,"{" . $connectstring . "}INBOX.Sent","$header\r\n" ."$mbody\r\n");

if (!$app) {
error("Email copying to Sent folder FAILED!");
owain at vaughan dot com
14 years ago
With SIMS IMAP server you also need to use \r\n as a line terminator, otherwise you will be able to add all the header lines correctly, but the body of the message will not be saved.<br>
You can use \n by itself for each header line, but when creating the blank line between the headers and the body you must use \r\n\r\n
serpent at paradise dot net dot nz
3 years ago
We couldn't get this function to work properly with MS-Exchange/IMAP, not because there's a problem with a the function as such, but because MS-Exchange changed the content-type in the header from multipart/[anything] to text/plain in every instance.  We set ImapMessagesRetrievalMimeFormat to HtmlAndTextAlternative
on the Exchange server, and that didn't help either.  I even tried wrapping the multipart message in a message/rfc822 wrapper and it reached inside the outer message and tweeked the inner message from multipart/alternative to text/plain.

Messages emailed to the mbox were unaffected, I suggest you try that route instead, and move the relevant messages to where you want them.
4 years ago
I encountered 3 problems when attempting to get my server to copy messages to the sent folder.

1. Wouldn't login until I added /novalidate-cert
2. No code example of getting the date sent
3. Including an attachment

Hope this code helps others

=date("d-M-Y H:i:s");
$dmy.= " +0100"; // Had to do this bit manually as server and me are in different timezones
$stream=@imap_open("{}INBOX.Sent", "username", "password");
$boundary = "------=".md5(uniqid(rand()));
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
$header .= "\r\n";
$ouv=fopen ("$file", "rb");$lir=fread ($ouv, filesize ("$file"));fclose
$attachment = chunk_split(base64_encode($lir));
$msg2 .= "--$boundary\r\n";
$msg2 .= "Content-Transfer-Encoding: base64\r\n";
$msg2 .= "Content-Disposition: attachment; filename=\"$filename\"\r\n";
$msg2 .= "\r\n";
$msg2 .= $attachment . "\r\n";
$msg2 .= "\r\n\r\n";
$msg3 .= "--$boundary--\r\n";
imap_append($stream,"{}INBOX.Sent","From: Somebody\r\n"."To:\r\n"."Date: $dmy\r\n"."Subject: This is the subject\r\n"."$header\r\n"."$msg2\r\n"."$msg3\r\n");
imap_close ($stream);
chris at sma-sportsmgmt dot com
8 years ago
I spend most of today refining the example to make it work properly when saving to the Inbox.Sent folder.

The append command now looks like:

    $return = imap_append($stream,$mailbox_addr
                       , "From: $fromReply\r\n"
                       . "To: $to\r\n"
                       . "Subject: $subject\r\n"
                       . "Date: $now  \r\n"
              . "X-Mailer: Cmail_v2.0 \r\n"
                       . "X-Originating-IP: $ip_addr \r\n"
                       . "MIME-Version: 1 \r\n"
                       . "Content-Type: text/html;\r\n\tcharset=\"ISO-8859-1\"\r\n"
                       . "Content-Transfer-Encoding: 8bit \r\n"
                       . "\r\n\r\n"
                       . "$wk_msg\r\n"

This inserts the current date into the email and lets it support html content. The one thing that I haven't got working yet is including attachments. I presumably have to make the boundaries and attachment content part of the message body.

jesper at angelo dot dk
16 years ago
Please observe that imap_append() do not support NNTP posting. Use fsockopen() instead, and do the work yourself.
To Top