Ignore:
Timestamp:
06/15/11 15:52:23 (13 years ago)
Author:
emersonfaria
Message:

Ticket #1963 - Foram implementadas correcoes em varias funcoes do backend imap e no mimeDecode.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/z-push/backend/imap.php

    r4219 r4613  
    9191         * the new message as any other new message in a folder. 
    9292         */ 
    93         function SendMail($rfc822, $forward = false, $reply = false, $parent = false) { 
    94                 debugLog("IMAP-SendMail: " . $rfc822 . "for: $forward   reply: $reply   parent: $parent" ); 
    95  
    96                 $mobj = new Mail_mimeDecode($rfc822); 
    97                 $message = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $rfc822, 'crlf' => "\n", 'charset' => 'utf-8')); 
    98  
    99                 $toaddr = $ccaddr = $bccaddr = ""; 
    100                 if(isset($message->headers["to"])) 
    101                 $toaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["to"])); 
    102                 if(isset($message->headers["cc"])) 
    103                 $ccaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["cc"])); 
    104                 if(isset($message->headers["bcc"])) 
    105                 $bccaddr = $this->parseAddr(Mail_RFC822::parseAddressList($message->headers["bcc"])); 
    106  
    107                 // save some headers when forwarding mails (content type & transfer-encoding) 
    108                 $headers = ""; 
    109                 $forward_h_ct = ""; 
    110                 $forward_h_cte = ""; 
    111  
    112                 $use_orgbody = false; 
    113  
    114                 // clean up the transmitted headers 
    115                 // remove default headers because we are using imap_mail 
    116                 $changedfrom = false; 
    117                 $returnPathSet = false; 
    118                 $body_base64 = false; 
    119                 $org_charset = ""; 
    120                 foreach($message->headers as $k => $v) { 
    121                         if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") 
    122                         continue; 
    123  
    124                         if ($k == "content-type") { 
    125                                 // save the original content-type header for the body part when forwarding 
    126                                 if ($forward) { 
    127                                         $forward_h_ct = $v; 
    128                                         continue; 
    129                                 } 
    130  
    131                                 // set charset always to utf-8 
    132                                 $org_charset = $v; 
    133                                 $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); 
    134                         } 
    135  
    136                         if ($k == "content-transfer-encoding") { 
    137                                 // if the content was base64 encoded, encode the body again when sending 
    138                                 if (trim($v) == "base64") $body_base64 = true; 
    139  
    140                                 // save the original encoding header for the body part when forwarding 
    141                                 if ($forward) { 
    142                                         $forward_h_cte = $v; 
    143                                         continue; 
    144                                 } 
    145                         } 
    146  
    147                         // if the message is a multipart message, then we should use the sent body 
    148                         if (!$forward && $k == "content-type" && preg_match("/multipart/i", $v)) { 
    149                                 $use_orgbody = true; 
    150                         } 
    151  
    152                         // check if "from"-header is set 
    153                         if ($k == "from" && ! trim($v) && IMAP_DEFAULTFROM) { 
    154                                 $changedfrom = true; 
    155                                 if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
    156                                 else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
    157                                 else $v = $this->_username . IMAP_DEFAULTFROM; 
    158                         } 
    159  
    160                         // check if "Return-Path"-header is set 
    161                         if ($k == "return-path") { 
    162                                 $returnPathSet = true; 
    163                                 if (! trim($v) && IMAP_DEFAULTFROM) { 
    164                                         if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
    165                                         else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
    166                                         else $v = $this->_username . IMAP_DEFAULTFROM; 
    167                                 } 
    168                         } 
    169  
    170                         // all other headers stay 
    171                         if ($headers) $headers .= "\n"; 
    172                         $headers .= ucfirst($k) . ": ". $v; 
    173                 } 
    174  
    175                 // set "From" header if not set on the device 
    176                 if(IMAP_DEFAULTFROM && !$changedfrom){ 
    177                         if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
    178                         else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
    179                         else $v = $this->_username . IMAP_DEFAULTFROM; 
    180                         if ($headers) $headers .= "\n"; 
    181                         $headers .= 'From: '.$v; 
    182                 } 
    183  
    184                 // set "Return-Path" header if not set on the device 
    185                 if(IMAP_DEFAULTFROM && !$returnPathSet){ 
    186                         if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
    187                         else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
    188                         else $v = $this->_username . IMAP_DEFAULTFROM; 
    189                         if ($headers) $headers .= "\n"; 
    190                         $headers .= 'Return-Path: '.$v; 
    191                 } 
    192  
    193                 // if this is a multipart message with a boundary, we must use the original body 
    194                 if ($use_orgbody) { 
    195                         list(,$body) = $mobj->_splitBodyHeader($rfc822); 
    196                 } 
    197                 else 
    198                 $body = $this->getBody($message); 
    199  
    200                 // reply 
    201                 if (isset($reply) && isset($parent) &&  $reply && $parent) { 
    202                         $this->imap_reopenFolder($parent); 
    203                         // receive entire mail (header + body) to decode body correctly 
    204                         $origmail = @imap_fetchheader($this->_mbox, $reply, FT_PREFETCHTEXT | FT_UID) . @imap_body($this->_mbox, $reply, FT_PEEK | FT_UID); 
    205                         $mobj2 = new Mail_mimeDecode($origmail); 
    206                         // receive only body 
    207                         $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'input' => $origmail, 'crlf' => "\n", 'charset' => 'utf-8'))); 
    208                         // unset mimedecoder & origmail - free memory 
    209                         unset($mobj2); 
    210                         unset($origmail); 
    211                 } 
    212  
    213                 // encode the body to base64 if it was sent originally in base64 by the pda 
    214                 // the encoded body is included in the forward 
    215                 if ($body_base64) $body = base64_encode($body); 
    216  
    217  
    218                 // forward 
    219                 if (isset($forward) && isset($parent) && $forward && $parent) { 
    220                         $this->imap_reopenFolder($parent); 
    221                         // receive entire mail (header + body) 
    222                         $origmail = @imap_fetchheader($this->_mbox, $forward, FT_PREFETCHTEXT | FT_UID) . @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID); 
    223  
    224                         // build a new mime message, forward entire old mail as file 
    225                         list($aheader, $body) = $this->mail_attach("forwarded_message.eml",strlen($origmail),$origmail, $body, $forward_h_ct, $forward_h_cte); 
    226  
    227                         // unset origmail - free memory 
    228                         unset($origmail); 
    229  
    230                         // add boundary headers 
    231                         $headers .= "\n" . $aheader; 
    232                 } 
    233  
    234                 //advanced debugging 
    235                 //debugLog("IMAP-SendMail: parsed message: ". print_r($message,1)); 
    236                 //debugLog("IMAP-SendMail: headers: $headers"); 
    237                 //debugLog("IMAP-SendMail: subject: {$message->headers["subject"]}"); 
    238                 //debugLog("IMAP-SendMail: body: $body"); 
    239  
    240                 $send =  @imap_mail ( $toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); 
    241  
    242                 // email sent? 
    243                 if (!$send) { 
    244                         debugLog("The email could not be sent. Last-IMAP-error: ". imap_last_error()); 
    245                 } 
    246  
    247                 // add message to the sent folder 
    248                 // build complete headers 
    249                 $cheaders  = "To: " . $toaddr. "\n"; 
    250                 $cheaders .= "Subject: " . $message->headers["subject"] . "\n"; 
    251                 $cheaders .= "Cc: " . $ccaddr . "\n"; 
    252                 $cheaders .= $headers; 
    253  
    254                 $asf = false; 
    255                 if ($this->_sentID) { 
    256                         $asf = $this->addSentMessage($this->_sentID, $cheaders, $body); 
    257                 } 
    258                 else if (IMAP_SENTFOLDER) { 
    259                         $asf = $this->addSentMessage(IMAP_SENTFOLDER, $cheaders, $body); 
    260                         debugLog("IMAP-SendMail: Outgoing mail saved in configured 'Sent' folder '".IMAP_SENTFOLDER."': ". (($asf)?"success":"failed")); 
    261                 } 
    262                 // No Sent folder set, try defaults 
    263                 else { 
    264                         debugLog("IMAP-SendMail: No Sent mailbox set"); 
    265                         if($this->addSentMessage("INBOX.Sent", $cheaders, $body)) { 
    266                                 debugLog("IMAP-SendMail: Outgoing mail saved in 'INBOX.Sent'"); 
    267                                 $asf = true; 
    268                         } 
    269                         else if ($this->addSentMessage("Sent", $cheaders, $body)) { 
    270                                 debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent'"); 
    271                                 $asf = true; 
    272                         } 
    273                         else if ($this->addSentMessage("Sent Items", $cheaders, $body)) { 
    274                                 debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); 
    275                                 $asf = true; 
    276                         } 
    277                 } 
    278  
    279                 // unset mimedecoder - free memory 
    280                 unset($mobj); 
    281                 return ($send && $asf); 
    282         } 
     93    function SendMail($rfc822, $forward = false, $reply = false, $parent = false) { 
     94        debugLog("IMAP-SendMail: for: $forward   reply: $reply   parent: $parent  RFC822:  \n". $rfc822 ); 
     95 
     96        $mobj = new Mail_mimeDecode($rfc822); 
     97        $message = $mobj->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); 
     98         
     99        $Mail_RFC822 = new Mail_RFC822(); 
     100        $toaddr = $ccaddr = $bccaddr = ""; 
     101        if(isset($message->headers["to"])) 
     102            $toaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["to"])); 
     103        if(isset($message->headers["cc"])) 
     104            $ccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["cc"])); 
     105        if(isset($message->headers["bcc"])) 
     106            $bccaddr = $this->parseAddr($Mail_RFC822->parseAddressList($message->headers["bcc"])); 
     107 
     108        // save some headers when forwarding mails (content type & transfer-encoding) 
     109        $headers = ""; 
     110        $forward_h_ct = ""; 
     111        $forward_h_cte = ""; 
     112        $envelopefrom = ""; 
     113 
     114        $use_orgbody = false; 
     115         
     116        // clean up the transmitted headers 
     117        // remove default headers because we are using imap_mail 
     118        $changedfrom = false; 
     119        $returnPathSet = false; 
     120        $body_base64 = false; 
     121        $org_charset = ""; 
     122        $org_boundary = false; 
     123        $multipartmixed = false; 
     124        foreach($message->headers as $k => $v) { 
     125            if ($k == "subject" || $k == "to" || $k == "cc" || $k == "bcc") 
     126                continue; 
     127 
     128            if ($k == "content-type") { 
     129                // if the message is a multipart message, then we should use the sent body 
     130                if (preg_match("/multipart/i", $v)) { 
     131                    $use_orgbody = true; 
     132                    $org_boundary = $message->ctype_parameters["boundary"]; 
     133                } 
     134 
     135                // save the original content-type header for the body part when forwarding 
     136                if ($forward && !$use_orgbody) { 
     137                    $forward_h_ct = $v; 
     138                    continue; 
     139                } 
     140 
     141                // set charset always to utf-8 
     142                $org_charset = $v; 
     143                $v = preg_replace("/charset=([A-Za-z0-9-\"']+)/", "charset=\"utf-8\"", $v); 
     144            } 
     145 
     146            if ($k == "content-transfer-encoding") { 
     147                // if the content was base64 encoded, encode the body again when sending 
     148                if (trim($v) == "base64") $body_base64 = true; 
     149 
     150                // save the original encoding header for the body part when forwarding 
     151                if ($forward) { 
     152                    $forward_h_cte = $v; 
     153                    continue; 
     154                } 
     155            } 
     156 
     157            // check if "from"-header is set, do nothing if it's set 
     158            // else set it to IMAP_DEFAULTFROM 
     159            if ($k == "from") { 
     160                if (trim($v)) { 
     161                    $changedfrom = true; 
     162                } elseif (! trim($v) && IMAP_DEFAULTFROM) { 
     163                    $changedfrom = true; 
     164                    if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
     165                    else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
     166                    else $v = $this->_username . IMAP_DEFAULTFROM; 
     167                    $envelopefrom = "-f$v"; 
     168                } 
     169            } 
     170 
     171            // check if "Return-Path"-header is set 
     172            if ($k == "return-path") { 
     173                $returnPathSet = true; 
     174                if (! trim($v) && IMAP_DEFAULTFROM) { 
     175                    if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
     176                    else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
     177                    else $v = $this->_username . IMAP_DEFAULTFROM; 
     178                } 
     179            } 
     180 
     181            // all other headers stay 
     182            if ($headers) $headers .= "\n"; 
     183            $headers .= ucfirst($k) . ": ". $v; 
     184        } 
     185 
     186        // set "From" header if not set on the device 
     187        if(IMAP_DEFAULTFROM && !$changedfrom){ 
     188            if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
     189            else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
     190            else $v = $this->_username . IMAP_DEFAULTFROM; 
     191            if ($headers) $headers .= "\n"; 
     192            $headers .= 'From: '.$v; 
     193            $envelopefrom = "-f$v"; 
     194        } 
     195 
     196        // set "Return-Path" header if not set on the device 
     197        if(IMAP_DEFAULTFROM && !$returnPathSet){ 
     198            if      (IMAP_DEFAULTFROM == 'username') $v = $this->_username; 
     199            else if (IMAP_DEFAULTFROM == 'domain')   $v = $this->_domain; 
     200            else $v = $this->_username . IMAP_DEFAULTFROM; 
     201            if ($headers) $headers .= "\n"; 
     202            $headers .= 'Return-Path: '.$v; 
     203        } 
     204 
     205        // if this is a multipart message with a boundary, we must use the original body 
     206        if ($use_orgbody) { 
     207            list(,$body) = $mobj->_splitBodyHeader($rfc822); 
     208            $repl_body = $this->getBody($message); 
     209        } 
     210        else 
     211            $body = $this->getBody($message); 
     212 
     213        // reply 
     214        if ($reply && $parent) { 
     215            $this->imap_reopenFolder($parent); 
     216            // receive entire mail (header + body) to decode body correctly 
     217            $origmail = @imap_fetchheader($this->_mbox, $reply, FT_UID) . @imap_body($this->_mbox, $reply, FT_PEEK | FT_UID); 
     218            $mobj2 = new Mail_mimeDecode($origmail); 
     219            // receive only body 
     220            $body .= $this->getBody($mobj2->decode(array('decode_headers' => false, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8'))); 
     221            // unset mimedecoder & origmail - free memory 
     222            unset($mobj2); 
     223            unset($origmail); 
     224        } 
     225 
     226        // encode the body to base64 if it was sent originally in base64 by the pda 
     227        // contrib - chunk base64 encoded body 
     228        if ($body_base64 && !$forward) $body = chunk_split(base64_encode($body)); 
     229 
     230 
     231        // forward 
     232        if ($forward && $parent) { 
     233            $this->imap_reopenFolder($parent); 
     234            // receive entire mail (header + body) 
     235            $origmail = @imap_fetchheader($this->_mbox, $forward, FT_UID) . @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID); 
     236 
     237            //if (!defined('IMAP_INLINE_FORWARD') || IMAP_INLINE_FORWARD === false) { 
     238            if (defined('IMAP_INLINE_FORWARD') && IMAP_INLINE_FORWARD === false) { 
     239                // contrib - chunk base64 encoded body 
     240                if ($body_base64) $body = chunk_split(base64_encode($body)); 
     241                //use original boundary if it's set 
     242                $boundary = ($org_boundary) ? $org_boundary : false; 
     243                // build a new mime message, forward entire old mail as file 
     244                list($aheader, $body) = $this->mail_attach("forwarded_message.eml",strlen($origmail),$origmail, $body, $forward_h_ct, $forward_h_cte,$boundary); 
     245                // add boundary headers 
     246                $headers .= "\n" . $aheader; 
     247 
     248            } 
     249            else { 
     250                $mobj2 = new Mail_mimeDecode($origmail); 
     251                $mess2 = $mobj2->decode(array('decode_headers' => true, 'decode_bodies' => true, 'include_bodies' => true, 'charset' => 'utf-8')); 
     252 
     253                if (!$use_orgbody) 
     254                    $nbody = $body; 
     255                else 
     256                    $nbody = $repl_body; 
     257 
     258                $nbody .= "\r\n\r\n"; 
     259                $nbody .= "-----Original Message-----\r\n"; 
     260                if(isset($mess2->headers['from'])) 
     261                    $nbody .= "From: " . $mess2->headers['from'] . "\r\n"; 
     262                if(isset($mess2->headers['to']) && strlen($mess2->headers['to']) > 0) 
     263                    $nbody .= "To: " . $mess2->headers['to'] . "\r\n"; 
     264                if(isset($mess2->headers['cc']) && strlen($mess2->headers['cc']) > 0) 
     265                    $nbody .= "Cc: " . $mess2->headers['cc'] . "\r\n"; 
     266                if(isset($mess2->headers['date'])) 
     267                    $nbody .= "Sent: " . $mess2->headers['date'] . "\r\n"; 
     268                if(isset($mess2->headers['subject'])) 
     269                    $nbody .= "Subject: " . $mess2->headers['subject'] . "\r\n"; 
     270                $nbody .= "\r\n"; 
     271                $nbody .= $this->getBody($mess2); 
     272 
     273                if ($body_base64) { 
     274                    // contrib - chunk base64 encoded body 
     275                    $nbody = chunk_split(base64_encode($nbody)); 
     276                    if ($use_orgbody) 
     277                    // contrib - chunk base64 encoded body 
     278                        $repl_body = chunk_split(base64_encode($repl_body)); 
     279                } 
     280 
     281                if ($use_orgbody) { 
     282                    debugLog("-------------------"); 
     283                    debugLog("old:\n'$repl_body'\nnew:\n'$nbody'\nund der body:\n'$body'"); 
     284                    //$body is quoted-printable encoded while $repl_body and $nbody are plain text, 
     285                    //so we need to decode $body in order replace to take place 
     286                    $body = str_replace($repl_body, $nbody, quoted_printable_decode($body)); 
     287                } 
     288                else 
     289                    $body = $nbody; 
     290 
     291 
     292                if(isset($mess2->parts)) { 
     293                    $attached = false; 
     294 
     295                    if ($org_boundary) { 
     296                        $att_boundary = $org_boundary; 
     297                        // cut end boundary from body 
     298                        $body = substr($body, 0, strrpos($body, "--$att_boundary--")); 
     299                    } 
     300                    else { 
     301                        $att_boundary = strtoupper(md5(uniqid(time()))); 
     302                        // add boundary headers 
     303                        $headers .= "\n" . "Content-Type: multipart/mixed; boundary=$att_boundary"; 
     304                        $multipartmixed = true; 
     305                    } 
     306 
     307                    foreach($mess2->parts as $part) { 
     308                        if(isset($part->disposition) && ($part->disposition == "attachment" || $part->disposition == "inline")) { 
     309 
     310                            if(isset($part->d_parameters['filename'])) 
     311                                $attname = $part->d_parameters['filename']; 
     312                            else if(isset($part->ctype_parameters['name'])) 
     313                                $attname = $part->ctype_parameters['name']; 
     314                            else if(isset($part->headers['content-description'])) 
     315                                $attname = $part->headers['content-description']; 
     316                            else $attname = "unknown attachment"; 
     317 
     318                            // ignore html content 
     319                            if ($part->ctype_primary == "text" && $part->ctype_secondary == "html") { 
     320                                continue; 
     321                            } 
     322                            // 
     323                            if ($use_orgbody || $attached) { 
     324                                $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body),$part->body, $part->ctype_primary ."/". $part->ctype_secondary); 
     325                            } 
     326                            // first attachment 
     327                            else { 
     328                                $encmail = $body; 
     329                                $attached = true; 
     330                                $body = $this->enc_multipart($att_boundary, $body, $forward_h_ct, $forward_h_cte); 
     331                                $body .= $this->enc_attach_file($att_boundary, $attname, strlen($part->body),$part->body, $part->ctype_primary ."/". $part->ctype_secondary); 
     332                            } 
     333                        } 
     334                    } 
     335                    if ($multipartmixed) { 
     336                        //this happens if a multipart/alternative message is forwarded 
     337                        //then it's a multipart/mixed message which consists of: 
     338                        //1. text/plain part which was written on the mobile 
     339                        //2. multipart/alternative part which is the original message 
     340                        //$body = "This is a message with multiple parts in MIME format.\n--". 
     341                        //        $att_boundary. 
     342                        //        "\nContent-Type: $forward_h_ct\nContent-Transfer-Encoding: $forward_h_cte\n\n". 
     343                        //        (($body_base64) ? chunk_split(base64_encode($message->body)) : rtrim($message->body)). 
     344                        //        "\n--".$att_boundary. 
     345                        //        "\nContent-Type: {$mess2->headers['content-type']}\n\n". 
     346                        //        @imap_body($this->_mbox, $forward, FT_PEEK | FT_UID)."\n\n"; 
     347                        $body = "\n--". 
     348                                $att_boundary. 
     349                                "\nContent-Type: $forward_h_ct\nContent-Transfer-Encoding: $forward_h_cte\n\n". 
     350                                $body; 
     351                    } 
     352 
     353                    $body .= "--$att_boundary--\n\n"; 
     354                } 
     355 
     356                unset($mobj2); 
     357            } 
     358 
     359            // unset origmail - free memory 
     360            unset($origmail); 
     361 
     362        } 
     363 
     364        // remove carriage-returns from body 
     365        $body = str_replace("\r\n", "\n", $body); 
     366 
     367        if (!$multipartmixed) { 
     368            if (!empty($forward_h_ct)) $headers .= "\nContent-Type: $forward_h_ct"; 
     369            if (!empty($forward_h_cte)) $headers .= "\nContent-Transfer-Encoding: $forward_h_cte"; 
     370        } 
     371        //advanced debugging 
     372        debugLog("IMAP-SendMail: parsed message: ". print_r($message,1)); 
     373        debugLog("IMAP-SendMail: headers: $headers"); 
     374        debugLog("IMAP-SendMail: subject: {$message->headers["subject"]}"); 
     375        debugLog("IMAP-SendMail: body: $body"); 
     376 
     377        if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { 
     378            $send =  @imap_mail ( $toaddr, $message->headers["subject"], $body, $headers, $ccaddr, $bccaddr); 
     379        } 
     380        else { 
     381            if (!empty($ccaddr))  $headers .= "\nCc: $ccaddr"; 
     382            if (!empty($bccaddr)) $headers .= "\nBcc: $bccaddr"; 
     383            $send =  @mail ( $toaddr, $message->headers["subject"], $body, $headers, $envelopefrom ); 
     384        } 
     385 
     386        // email sent? 
     387        if (!$send) { 
     388            debugLog("The email could not be sent. Last-IMAP-error: ". imap_last_error()); 
     389        } 
     390 
     391        // add message to the sent folder 
     392        // build complete headers 
     393        $headers .= "\nTo: $toaddr"; 
     394        $headers .= "\nSubject: " . $message->headers["subject"]; 
     395 
     396        if (!defined('IMAP_USE_IMAPMAIL') || IMAP_USE_IMAPMAIL == true) { 
     397            if (!empty($ccaddr))  $headers .= "\nCc: $ccaddr"; 
     398            if (!empty($bccaddr)) $headers .= "\nBcc: $bccaddr"; 
     399        } 
     400        debugLog("IMAP-SendMail: complete headers: $headers"); 
     401 
     402        $asf = false; 
     403        if ($this->_sentID) { 
     404            $asf = $this->addSentMessage($this->_sentID, $headers, $body); 
     405        } 
     406        else if (IMAP_SENTFOLDER) { 
     407            $asf = $this->addSentMessage(IMAP_SENTFOLDER, $headers, $body); 
     408            debugLog("IMAP-SendMail: Outgoing mail saved in configured 'Sent' folder '".IMAP_SENTFOLDER."': ". (($asf)?"success":"failed")); 
     409        } 
     410        // No Sent folder set, try defaults 
     411        else { 
     412            debugLog("IMAP-SendMail: No Sent mailbox set"); 
     413            if($this->addSentMessage("INBOX.Sent", $headers, $body)) { 
     414                debugLog("IMAP-SendMail: Outgoing mail saved in 'INBOX.Sent'"); 
     415                $asf = true; 
     416            } 
     417            else if ($this->addSentMessage("Sent", $headers, $body)) { 
     418                debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent'"); 
     419                $asf = true; 
     420            } 
     421            else if ($this->addSentMessage("Sent Items", $headers, $body)) { 
     422                debugLog("IMAP-SendMail: Outgoing mail saved in 'Sent Items'"); 
     423                $asf = true; 
     424            } 
     425        } 
     426 
     427        // unset mimedecoder - free memory 
     428        unset($mobj); 
     429        return ($send && $asf); 
     430    } 
     431         
    283432 
    284433        /* Should return a wastebasket folder if there is one. This is used when deleting 
     
    9201069 
    9211070        // build a multipart email, embedding body and one file (for attachments) 
    922         function mail_attach($filenm,$filesize,$file_cont,$body, $body_ct, $body_cte) { 
    923  
    924                 $boundary = strtoupper(md5(uniqid(time()))); 
    925  
    926                 $mail_header = "Content-Type: multipart/mixed; boundary=$boundary\n"; 
    927  
    928                 // build main body with the sumitted type & encoding from the pda 
    929                 $mail_body  = "This is a multi-part message in MIME format\n\n"; 
    930                 $mail_body .= "--$boundary\n"; 
    931                 $mail_body .= "Content-Type:$body_ct\n"; 
    932                 $mail_body .= "Content-Transfer-Encoding:$body_cte\n\n"; 
    933                 $mail_body .= "$body\n\n"; 
    934  
    935                 $mail_body .= "--$boundary\n"; 
    936                 $mail_body .= "Content-Type: text/plain; name=\"$filenm\"\n"; 
    937                 $mail_body .= "Content-Transfer-Encoding: base64\n"; 
    938                 $mail_body .= "Content-Disposition: attachment; filename=\"$filenm\"\n"; 
    939                 $mail_body .= "Content-Description: $filenm\n\n"; 
    940                 $mail_body .= base64_encode($file_cont) . "\n\n"; 
    941  
    942                 $mail_body .= "--$boundary--\n\n"; 
    943  
    944                 return array($mail_header, $mail_body); 
    945         } 
    946  
     1071   function mail_attach($filenm,$filesize,$file_cont,$body, $body_ct, $body_cte, $boundary = false) { 
     1072        if (!$boundary) $boundary = strtoupper(md5(uniqid(time()))); 
     1073 
     1074        //remove the ending boundary because we will add it at the end 
     1075        $body = str_replace("--$boundary--", "", $body); 
     1076 
     1077        $mail_header = "Content-Type: multipart/mixed; boundary=$boundary\n"; 
     1078 
     1079        // build main body with the sumitted type & encoding from the pda 
     1080        $mail_body  = $this->enc_multipart($boundary, $body, $body_ct, $body_cte); 
     1081        $mail_body .= $this->enc_attach_file($boundary, $filenm, $filesize, $file_cont); 
     1082 
     1083        $mail_body .= "--$boundary--\n\n"; 
     1084        return array($mail_header, $mail_body); 
     1085    } 
     1086 
     1087    function enc_multipart($boundary, $body, $body_ct, $body_cte) { 
     1088//        $mail_body = "This is a multi-part message in MIME format\n\n";    
     1089//        $mail_body .= "--$boundary\n"; 
     1090//        $mail_body .= "Content-Type: $body_ct\n"; 
     1091//        $mail_body .= "Content-Transfer-Encoding: $body_cte\n\n"; 
     1092        $mail_body = "$body\n\n"; 
     1093 
     1094        return $mail_body; 
     1095    } 
     1096     
     1097    function enc_attach_file($boundary, $filenm, $filesize, $file_cont, $content_type = "") { 
     1098        if (!$content_type) $content_type = "text/plain"; 
     1099        $mail_body = "--$boundary\n"; 
     1100        $mail_body .= "Content-Type: $content_type; name=\"$filenm\"\n"; 
     1101        $mail_body .= "Content-Transfer-Encoding: base64\n"; 
     1102        $mail_body .= "Content-Disposition: attachment; filename=\"$filenm\"\n"; 
     1103        $mail_body .= "Content-Description: $filenm\n\n"; 
     1104        //contrib - chunk base64 encoded attachments 
     1105        $mail_body .= chunk_split(base64_encode($file_cont)) . "\n\n"; 
     1106 
     1107        return $mail_body; 
     1108    } 
     1109         
    9471110        // adds a message as seen to a specified folder (used for saving sent mails) 
    9481111        function addSentMessage($folderid, $header, $body) { 
Note: See TracChangeset for help on using the changeset viewer.