Exemplo n.º 1
0
 function compose($text = null, $html = null, $attach = null, $uniq = null, $end = null, $debug = null)
 {
     if (!FUNC4::is_debug($debug)) {
         $debug = debug_backtrace();
     }
     $_mime = new MIME4();
     $err = array();
     if ($text == null && $html == null) {
         $err[] = 'message is not set';
     } else {
         if ($text != null) {
             if (!(is_array($text) && isset($text['header'], $text['content']) && is_string($text['header']) && is_string($text['content']) && MIME4::isset_header($text['header'], 'content-type', 'text/plain', $debug))) {
                 $err[] = 'invalid text message type';
             }
         }
         if ($html != null) {
             if (!(is_array($html) && isset($html['header'], $html['content']) && is_string($html['header']) && is_string($html['content']) && MIME4::isset_header($html['header'], 'content-type', 'text/html', $debug))) {
                 $err[] = 'invalid html message type';
             }
         }
     }
     if ($attach != null) {
         if (is_array($attach) && count($attach) > 0) {
             foreach ($attach as $arr) {
                 if (!(is_array($arr) && isset($arr['header'], $arr['content']) && is_string($arr['header']) && is_string($arr['content']) && (MIME4::isset_header($arr['header'], 'content-disposition', 'inline', $debug) || MIME4::isset_header($arr['header'], 'content-disposition', 'attachment', $debug)))) {
                     $err[] = 'invalid attachment type';
                     break;
                 }
             }
         } else {
             $err[] = 'invalid attachment format';
         }
     }
     if ($end == null) {
         $end = $_mime->LE;
     } else {
         if (!is_string($end)) {
             $err[] = 'invalid line end value';
         }
     }
     if (count($err) > 0) {
         FUNC4::trace($debug, implode(', ', $err));
     } else {
         $multipart = false;
         if ($text && $html) {
             $multipart = true;
         }
         if ($attach) {
             $multipart = true;
         }
         $header = $body = array();
         $header[] = 'Date: ' . date('r');
         $header[] = base64_decode('WC1NYWlsZXI6IFhQTTQgdi4wLjIgPCB3d3cueHBlcnRtYWlsZXIuY29tID4=');
         if ($multipart) {
             $uniq = $uniq == null ? 0 : intval($uniq);
             $boundary1 = '=_1.' . MIME4::unique($uniq++);
             $boundary2 = '=_2.' . MIME4::unique($uniq++);
             $boundary3 = '=_3.' . MIME4::unique($uniq++);
             $disp['inline'] = $disp['attachment'] = false;
             if ($attach != null) {
                 foreach ($attach as $darr) {
                     if (MIME4::isset_header($darr['header'], 'content-disposition', 'inline', $debug)) {
                         $disp['inline'] = true;
                     } else {
                         if (MIME4::isset_header($darr['header'], 'content-disposition', 'attachment', $debug)) {
                             $disp['attachment'] = true;
                         }
                     }
                 }
             }
             $hstr = 'Content-Type: multipart/%s;' . $end . "\t" . 'boundary="%s"';
             $bstr = '--%s' . $end . '%s' . $end . $end . '%s';
             $header[] = 'MIME-Version: 1.0';
             $body[] = 'This is a message in MIME Format. If you see this, your mail reader does not support this format.' . $end;
             if ($text && $html) {
                 if ($disp['inline'] && $disp['attachment']) {
                     $header[] = sprintf($hstr, 'mixed', $boundary1);
                     $body[] = '--' . $boundary1;
                     $body[] = sprintf($hstr, 'related', $boundary2) . $end;
                     $body[] = '--' . $boundary2;
                     $body[] = sprintf($hstr, 'alternative', $boundary3) . $end;
                     $body[] = sprintf($bstr, $boundary3, $text['header'], $text['content']);
                     $body[] = sprintf($bstr, $boundary3, $html['header'], $html['content']);
                     $body[] = '--' . $boundary3 . '--';
                     foreach ($attach as $desc) {
                         if (MIME4::isset_header($desc['header'], 'content-disposition', 'inline', $debug)) {
                             $body[] = sprintf($bstr, $boundary2, $desc['header'], $desc['content']);
                         }
                     }
                     $body[] = '--' . $boundary2 . '--';
                     foreach ($attach as $desc) {
                         if (MIME4::isset_header($desc['header'], 'content-disposition', 'attachment', $debug)) {
                             $body[] = sprintf($bstr, $boundary1, $desc['header'], $desc['content']);
                         }
                     }
                     $body[] = '--' . $boundary1 . '--';
                 } else {
                     if ($disp['inline']) {
                         $header[] = sprintf($hstr, 'related', $boundary1);
                         $body[] = '--' . $boundary1;
                         $body[] = sprintf($hstr, 'alternative', $boundary2) . $end;
                         $body[] = sprintf($bstr, $boundary2, $text['header'], $text['content']);
                         $body[] = sprintf($bstr, $boundary2, $html['header'], $html['content']);
                         $body[] = '--' . $boundary2 . '--';
                         foreach ($attach as $desc) {
                             $body[] = sprintf($bstr, $boundary1, $desc['header'], $desc['content']);
                         }
                         $body[] = '--' . $boundary1 . '--';
                     } else {
                         if ($disp['attachment']) {
                             $header[] = sprintf($hstr, 'mixed', $boundary1);
                             $body[] = '--' . $boundary1;
                             $body[] = sprintf($hstr, 'alternative', $boundary2) . $end;
                             $body[] = sprintf($bstr, $boundary2, $text['header'], $text['content']);
                             $body[] = sprintf($bstr, $boundary2, $html['header'], $html['content']);
                             $body[] = '--' . $boundary2 . '--';
                             foreach ($attach as $desc) {
                                 $body[] = sprintf($bstr, $boundary1, $desc['header'], $desc['content']);
                             }
                             $body[] = '--' . $boundary1 . '--';
                         } else {
                             $header[] = sprintf($hstr, 'alternative', $boundary1);
                             $body[] = sprintf($bstr, $boundary1, $text['header'], $text['content']);
                             $body[] = sprintf($bstr, $boundary1, $html['header'], $html['content']);
                             $body[] = '--' . $boundary1 . '--';
                         }
                     }
                 }
             } else {
                 if ($text) {
                     $header[] = sprintf($hstr, 'mixed', $boundary1);
                     $body[] = sprintf($bstr, $boundary1, $text['header'], $text['content']);
                     foreach ($attach as $desc) {
                         $body[] = sprintf($bstr, $boundary1, $desc['header'], $desc['content']);
                     }
                     $body[] = '--' . $boundary1 . '--';
                 } else {
                     if ($html) {
                         if ($disp['inline'] && $disp['attachment']) {
                             $header[] = sprintf($hstr, 'mixed', $boundary1);
                             $body[] = '--' . $boundary1;
                             $body[] = sprintf($hstr, 'related', $boundary2) . $end;
                             $body[] = sprintf($bstr, $boundary2, $html['header'], $html['content']);
                             foreach ($attach as $desc) {
                                 if (MIME4::isset_header($desc['header'], 'content-disposition', 'inline', $debug)) {
                                     $body[] = sprintf($bstr, $boundary2, $desc['header'], $desc['content']);
                                 }
                             }
                             $body[] = '--' . $boundary2 . '--';
                             foreach ($attach as $desc) {
                                 if (MIME4::isset_header($desc['header'], 'content-disposition', 'attachment', $debug)) {
                                     $body[] = sprintf($bstr, $boundary1, $desc['header'], $desc['content']);
                                 }
                             }
                             $body[] = '--' . $boundary1 . '--';
                         } else {
                             if ($disp['inline']) {
                                 $header[] = sprintf($hstr, 'related', $boundary1);
                                 $body[] = sprintf($bstr, $boundary1, $html['header'], $html['content']);
                                 foreach ($attach as $desc) {
                                     $body[] = sprintf($bstr, $boundary1, $desc['header'], $desc['content']);
                                 }
                                 $body[] = '--' . $boundary1 . '--';
                             } else {
                                 if ($disp['attachment']) {
                                     $header[] = sprintf($hstr, 'mixed', $boundary1);
                                     $body[] = sprintf($bstr, $boundary1, $html['header'], $html['content']);
                                     foreach ($attach as $desc) {
                                         $body[] = sprintf($bstr, $boundary1, $desc['header'], $desc['content']);
                                     }
                                     $body[] = '--' . $boundary1 . '--';
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if ($text) {
                 $header[] = $text['header'];
                 $body[] = $text['content'];
             } else {
                 if ($html) {
                     $header[] = $html['header'];
                     $body[] = $html['content'];
                 }
             }
         }
         return array('header' => implode($end, $header), 'content' => implode($end, $body));
     }
 }