imap_append

(PHP 4, PHP 5)

imap_appendДобавляет строковое сообщение в указанный почтовый ящик

Описание

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

Добавляет строку message в указанный mailbox.

Список параметров

imap_stream

Поток IMAP, полученный из imap_open().

mailbox

Имя почтового ящика. Cмотрите imap_open() для подробной информации.

message

Добавляемое сообщение в виде строки

При обращении к Cyrus IMAP серверу следует использовать "\r\n" как завершающий символ строки вместо "\n", иначе операция будет неудачна.

options

Если указан, то параметр options также будет записан в mailbox

internal_date

Если этот параметр указан, он установит INTERNALDATE в добавляемом сообщении. Параметр должен содержать дату, представленную строкой, которая соответствует спецификации rfc2060 для значения date_time.

Возвращаемые значения

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Список изменений

Версия Описание
5.3.2 Добавлена поддержка INTERNALDATE для imap_append.

Примеры

Пример #1 Пример использования imap_append()

<?php
$stream 
imap_open("{imap.example.org}INBOX.Drafts""username""password");

$check imap_check($stream);
echo 
"Кол-во сообщений до добавления: "$check->Nmsgs "\n";

imap_append($stream"{imap.example.org}INBOX.Drafts"
                   
"From: me@example.com\r\n"
                   
"To: you@example.com\r\n"
                   
"Subject: test\r\n"
                   
"\r\n"
                   
"это проверочное сообщение, пожалуйста, игнорируйте его\r\n"
                   
);

$check imap_check($stream);
echo 
"Кол-во сообщений после добавления : "$check->Nmsgs "\n";

imap_close($stream);
?>

Коментарии

Please observe that imap_append() do not support NNTP posting. Use fsockopen() instead, and do the work yourself.
1999-09-14 12:13:56
http://php5.kiev.ua/manual/ru/function.imap-append.html
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
2001-08-01 09:49:39
http://php5.kiev.ua/manual/ru/function.imap-append.html
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!");
}
2002-06-12 03:24:56
http://php5.kiev.ua/manual/ru/function.imap-append.html
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
2003-04-29 10:29:56
http://php5.kiev.ua/manual/ru/function.imap-append.html
Автор:
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.
2003-06-23 18:26:19
http://php5.kiev.ua/manual/ru/function.imap-append.html
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 http://www.zend.com/lists/php-dev/200303/msg00843.html 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"
2007-01-17 09:57:35
http://php5.kiev.ua/manual/ru/function.imap-append.html
The last argument, $options, are flags like for use with imap_setflag_full.
It took a while before I found out
2007-05-03 10:36:41
http://php5.kiev.ua/manual/ru/function.imap-append.html
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.

Chris
2008-02-08 01:06:28
http://php5.kiev.ua/manual/ru/function.imap-append.html
The date format string to use when creating $internal_date is 'd-M-Y H:i:s O'.
2010-08-15 23:52:46
http://php5.kiev.ua/manual/ru/function.imap-append.html
Автор:
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

<?php
$dmy
=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("{mail.example.com/novalidate-cert}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";
$file="../path_to/filename.pdf";
$filename="filename.pdf";
$ouv=fopen ("$file""rb");$lir=fread ($ouvfilesize ("$file"));fclose
($ouv);
$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,"{mail.example.com/novalidate-cert}INBOX.Sent","From: Somebody\r\n"."To: test@example.co.uk\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);
?>
2011-08-08 12:30:45
http://php5.kiev.ua/manual/ru/function.imap-append.html
Here is how I used this function with an attachment. I used examples from everyone else but made it easier and cleaner to change.

<?php
$authhost
="{000.000.000.000:993/validate-cert/ssl}Drafts";

$user="sadasd";
$pass="sadasd";

if (
$mbox=imap_open$authhost$user$pass))
{
   
$dmy=date("d-M-Y H:i:s");
   
   
$filename="filename.pdf";
   
$attachment chunk_split(base64_encode($filestring));
   
   
$boundary "------=".md5(uniqid(rand()));
   
   
$msg = ("From: Somebody\r\n"
       
"To: test@example.co.uk\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"); 

   
imap_close($mbox);
}
else
{
    echo 
"<h1>FAIL!</h1>\n";
}

?>

Hope this helps someone.
2011-11-23 13:45:53
http://php5.kiev.ua/manual/ru/function.imap-append.html
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.
2013-02-06 22:10:29
http://php5.kiev.ua/manual/ru/function.imap-append.html
Hi,

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:

imap_append($imap_conn,$mail_box,$msg."\r\n","\\Seen");

I hope this helps the readers, and prevents saves people from doing what we started doing - hand crafting the MIME messages :-0
2013-02-21 22:59:58
http://php5.kiev.ua/manual/ru/function.imap-append.html
Автор:
You can use PHPMailer ( https://github.com/PHPMailer/PHPMailer/ ) with imap.

<?php
// after creating content of mail you have to run preSend() - part of send() method
$mail->send();
// and you can get whole raw message with getSentMIMEMessage() method
imap_append($imap$mailserver.'INBOX.Sent',$mail->getSentMIMEMessage(), "\\Seen");
2015-05-15 15:18:12
http://php5.kiev.ua/manual/ru/function.imap-append.html
$fn = 'filename.zip';
$IMAPhost = 'imap.hostname.com';
// IMAP port 143, 995 or POP3 port 110, 995
$IMAPport = '143';
$IMAPssl = 'tls';
$IMAP = '{'.$IMAPhost.':'.$IMAPport.'/imap/'.$IMAPssl.'/novalidate-cert}';
$IMAPuser = 'hello@hostname.com';       
$msg ='Message text <h1 style="color: #093"> Hello </h1>';
// imap email and password
$ibox = imap_open($IMAP, $_SESSION['email'], $_SESSION['pass']);
$dmy=date("Y-m-d H:i:s");
// pack file contents
$attachment = chunk_split(base64_encode(file_get_contents('tmp/'.$fn)));   
$boundary = "------=".md5(uniqid(rand())); 
imap_append($ibox, $IMAP."Send"
            , "From: Hello <".$IMAPuser.">\r\n"
            . "To: ".$email."\r\n"
            . "Date: $dmy\r\n" 
            . "Subject: ".$sub."\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=\"utf-8\"\r\n"
                . "Content-Transfer-Encoding: 8bit \r\n"
                . "\r\n\r\n" 
            . html_entity_decode($msg)."\r\n"
                . "\r\n\r\n"
                . "--$boundary\r\n"
                . "Content-Transfer-Encoding: base64\r\n"
                . "Content-Disposition: attachment; filename=\"$fn\"\r\n"
                . "\r\n" . $attachment . "\r\n"
                . "\r\n\r\n\r\n"   
                . "--$boundary--\r\n\r\n" 
);
2016-08-19 11:15:40
http://php5.kiev.ua/manual/ru/function.imap-append.html
// this works for me, better version :]. 
<?php
$fn 
'filename.zip';
$IMAPhost 'imap.hostname.com';
// IMAP port 143, 995 or POP3 port 110, 995
$IMAPport '143';
$IMAPssl 'tls';
$IMAP '{'.$IMAPhost.':'.$IMAPport.'/imap/'.$IMAPssl.'/novalidate-cert}';
$IMAPuser 'hello@hostname.com';       
$msg ='Message text <h1 style="color: #093"> Hello </h1>';
// imap email and password
$ibox imap_open($IMAP$_SESSION['email'], $_SESSION['pass']);
$dmy=date("Y-m-d H:i:s");
// pack file contents
$attachment chunk_split(base64_encode(file_get_contents('tmp/'.$fn))); 

       
$boundary1 "###".md5(microtime())."###";
       
$boundary2 "###".md5(microtime().rand(99,999))."###"
       
imap_append($ibox$IMAP."Send"
                     
"From: Hello <".$IMAPuser.">\r\n"
           
"To: ".$email."\r\n"
           
"Date: $dmy\r\n" 
           
"Subject: ".quoted_printable_encode($sub)."\r\n"
               
"MIME-Version: 1.0\r\n"
               
"Content-Type: multipart/mixed; boundary=\"$boundary1\"\r\n"
               
"\r\n\r\n"
               
"--$boundary1\r\n"
               
"Content-Type: multipart/alternative; boundary=\"$boundary2\"\r\n"
               
"\r\n\r\n"
               
// ADD Plain text data
               
"--$boundary2\r\n"
               
"Content-Type: text/plain; charset=\"utf-8\"\r\n"
               
"Content-Transfer-Encoding: quoted-printable\r\n"
               
"\r\n\r\n" 
               
$msg."\r\n"
               
"\r\n\r\n"
               
// ADD Html content
               
"--$boundary2\r\n"
               
"Content-Type: text/html; charset=\"utf-8\"\r\n"
               
"Content-Transfer-Encoding: quoted-printable \r\n"
               
"\r\n\r\n" 
               
html_entity_decode($msg)."\r\n"
               
"\r\n\r\n"
               
"--$boundary2\r\n"
               
"\r\n\r\n"
               
// ADD attachment(s)
               
"--$boundary1\r\n"
               
"Content-Type: image/gif; name=\"$fn\"\r\n"
               
"Content-Transfer-Encoding: base64\r\n"
               
"Content-Disposition: attachment; filename=\"$fn\"\r\n"
               
"\r\n\r\n"
               
$attachment 
               
"\r\n\r\n"   
               
"--$boundary1--\r\n\r\n" 
       
);

// nigdy więcej takich dziwactw :) ble..
?>
2016-08-20 21:38:18
http://php5.kiev.ua/manual/ru/function.imap-append.html
Mime message example (smtp telnet DATA example):

From: from@ccc.cc
To: to@@ccc.cc
Subject: Example Email
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MixedBoundaryString"

--MixedBoundaryString
Content-Type: multipart/related; boundary="RelatedBoundaryString"

--RelatedBoundaryString
Content-Type: multipart/alternative; boundary="AlternativeBoundaryString"

--AlternativeBoundaryString
Content-Type: text/plain;charset="utf-8"
Content-Transfer-Encoding: quoted-printable

This is the plain text part of the email.

--AlternativeBoundaryString
Content-Type: text/html;charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<html>
  <body>=0D
    <img src=3D=22cid:masthead.png=40ccc.cc=22 width 800 height=3D80=
 =5C>=0D
    <p>This is the html part of the email.</p>=0D
    <img src=3D=22cid:logo.png=40ccc.cc=22 width 200 height=3D60 =5C=
>=0D
  </body>=0D
</html>=0D

--AlternativeBoundaryString--

--RelatedBoundaryString
Content-Type: image/jpgeg;name="logo.png"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename="logo.png"
Content-ID: <logo.png@ccc.cc>

amtsb2hiaXVvbHJueXZzNXQ2XHVmdGd5d2VoYmFmaGpremxidTh2b2hydHVqd
dWkgb2l1b3NydGhpdXRvZ2hqdWlyb2h5dWd0aXJlaHN1aWhndXNpaHhidnVqZ
a25qbW9nNXRwbF0nemVycHpvemlnc3k5aDZqcm9wdHo7amlodDhpOTA4N3U5
LGZ2cDBbZWRzcm85eWo1Zmtsc2xrZ3g=

--RelatedBoundaryString
Content-Type: image/jpgeg;name="masthead.png"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename="masthead.png"
Content-ID: <masthead.png@ccc.cc>

aXR4ZGh5Yjd1OHk3MzQ4eXFndzhpYW9wO2tibHB6c2tqOTgwNXE0aW9qYWJ6aX
dGlmOTA0cW05dGkwbWk0OXQwYVttaXZvcnBhXGtsbGo7emt2c2pkZnI7Z2lwb2F
eXN6dWdoeXhiNzhuZzdnaHQ3eW9zemlqb2FqZWt0cmZ1eXZnamhka3JmdDg3aXV2dWd5aGVidXdz
dhyuhehe76YTGSFGA=

--RelatedBoundaryString--

--MixedBoundaryString
Content-Type: application/pdf;name="Invoice_1.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="Invoice_1.pdf"

aGZqZGtsZ3poZHVpeWZoemd2dXNoamRibngganZodWpyYWRuIHVqO0hmSjtyRVVPIEZSO05SVURF
SEx1aWhudWpoZ3h1XGh1c2loZWRma25kamlsXHpodXZpZmhkcnVsaGpnZmtsaG
a2psajY1ZWxqanNveHV5ZXJ3NTQzYXRnZnJhZXdhcmV0eXRia2xhanNueXVpNjRv

--MixedBoundaryString
Content-Type: application/pdf;name="SpecialOffer.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="SpecialOffer.pdf"

aXBvY21odWl0dnI1dWk4OXdzNHU5NTgwcDN3YTt1OTQwc3U4NTk1dTg0dTV5OG
cS0zNHU4NTk0eWI4OTcwdjg5MHE4cHV0O3BvYTt6dWI7dWlvenZ1em9pdW51dD
OTViOHk5cDV3dTh5bnB3dWZ2OHQ5dTh2cHVpO2p2Ymd1eTg5MGg3ajY4bjZ2ODl1ZGlvcjQ1amts
dfnhgjdfihn=

--MixedBoundaryString--

.

// Or you can download from
https://www.phpclasses.org/browse/file/14672.html
2017-08-14 12:22:30
http://php5.kiev.ua/manual/ru/function.imap-append.html
From my testing today, if using PHPMailer to send to an Exchange based IMAP server with an attachment or embedded image greater than 7kb, you will need to change the line returns from unix format to windows format. I simply did a string replace on '\n' and replaced with '\r\n' to get this done.

$content =  str_replace("\n", "\r\n", $mail->getSentMIMEMessage());
$result = imap_append($imapStream, $path, $content);
2018-12-13 12:02:14
http://php5.kiev.ua/manual/ru/function.imap-append.html

    Поддержать сайт на родительском проекте КГБ