Advertisement
Guest User

Untitled

a guest
Mar 27th, 2012
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.87 KB | None | 0 0
  1.  
  2. class MY_Email extends CI_Email
  3. {
  4.     protected function matched_replace_between($data, $delimiter1, $delimiter2, $match, $replace, $matchtype = 0)
  5.     {
  6.         $offset = 0;
  7.         $mlen = strlen($match);
  8.         while (($srcs = strpos($data, $delimiter1, $offset)) !== false)
  9.         {
  10.             $next2 = $srcs + strlen($delimiter1);
  11.             $next = $next2 - 1;
  12.             if (($srct = strpos($data, $delimiter2, $next2)) !== false)
  13.             {
  14.                 $sample = substr($data, $next2, $srct - $next2);
  15.                 $matched = (($matchtype === 0) && ($sample == $match));
  16.                 $matched = (($matchtype === 1) && (substr($sample, 0, $mlen)  == $match));
  17.                 $matched = (($matchtype === 2) && (substr($sample, $mlen)  == $match));
  18.                 $matched = (($matchtype === 3) && (strpos($sample, $match) !== false));
  19.                 if ($matched)
  20.                 {
  21.                     $data = substr($data, 0, $next2).$replace.substr($data, $srct);
  22.                     $offset = $srct + 1;
  23.                 }
  24.                 else
  25.                 {
  26.                     $offset = $next2;
  27.                 }
  28.             }
  29.             else
  30.             {
  31.                 $offset = $next2;
  32.             }
  33.         }
  34.         return $data;
  35.     }
  36.    
  37.     protected function _build_message()
  38.     {
  39.         if ($this->wordwrap === TRUE  AND  $this->mailtype != 'html')
  40.         {
  41.             $this->_body = $this->word_wrap($this->_body);
  42.         }
  43.  
  44.         $this->_set_boundaries();
  45.         $this->_write_headers();
  46.  
  47.         $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
  48.         $body = '';
  49.  
  50.         switch ($this->_get_content_type())
  51.         {
  52.             case 'plain' :
  53.  
  54.                 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  55.                 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
  56.  
  57.                 if ($this->_get_protocol() == 'mail')
  58.                 {
  59.                     $this->_header_str .= $hdr;
  60.                     $this->_finalbody = $this->_body;
  61.                 }
  62.                 else
  63.                 {
  64.                     $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
  65.                 }
  66.  
  67.                 return;
  68.  
  69.             break;
  70.             case 'html' :
  71.  
  72.                 if ($this->send_multipart === FALSE)
  73.                 {
  74.                     $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
  75.                     $hdr .= "Content-Transfer-Encoding: quoted-printable";
  76.                 }
  77.                 else
  78.                 {
  79.                     $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
  80.  
  81.                     $body .= $this->_get_mime_message() . $this->newline . $this->newline;
  82.                     $body .= "--" . $this->_alt_boundary . $this->newline;
  83.  
  84.                     $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  85.                     $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
  86.                     $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
  87.  
  88.                     $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
  89.                     $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
  90.                 }
  91.  
  92.                 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
  93.  
  94.  
  95.                 if ($this->_get_protocol() == 'mail')
  96.                 {
  97.                     $this->_header_str .= $hdr;
  98.                 }
  99.                 else
  100.                 {
  101.                     $this->_finalbody = $hdr . $this->_finalbody;
  102.                 }
  103.  
  104.  
  105.                 if ($this->send_multipart !== FALSE)
  106.                 {
  107.                     $this->_finalbody .= "--" . $this->_alt_boundary . "--";
  108.                 }
  109.  
  110.                 return;
  111.  
  112.             break;
  113.             case 'plain-attach' :
  114.  
  115.                 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
  116.  
  117.                 if ($this->_get_protocol() == 'mail')
  118.                 {
  119.                     $this->_header_str .= $hdr;
  120.                 }
  121.  
  122.                 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
  123.                 $body .= "--" . $this->_atc_boundary . $this->newline;
  124.  
  125.                 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  126.                 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
  127.  
  128.                 $body .= $this->_body . $this->newline . $this->newline;
  129.  
  130.             break;
  131.             case 'html-attach' :
  132.  
  133.                 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
  134.  
  135.                 if ($this->_get_protocol() == 'mail')
  136.                 {
  137.                     $this->_header_str .= $hdr;
  138.                 }
  139.  
  140.                 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
  141.                 $body .= "--" . $this->_atc_boundary . $this->newline;
  142.  
  143.                 $body .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
  144.                 $body .= "--" . $this->_alt_boundary . $this->newline;
  145.  
  146.                 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  147.                 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
  148.                 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
  149.  
  150.                 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
  151.                 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
  152.  
  153.                 $body .= $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
  154.                 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
  155.  
  156.             break;
  157.         }
  158.  
  159.         $attachment = array();
  160.  
  161.         $z = 0;
  162.  
  163.         for ($i=0; $i < count($this->_attach_name); $i++)
  164.         {
  165.             $filename = $this->_attach_name[$i];
  166.             $basename = basename($filename);
  167.             $ctype = $this->_attach_type[$i];
  168.  
  169.             if ( ! file_exists($filename))
  170.             {
  171.                 $this->_set_error_message('lang:email_attachment_missing', $filename);
  172.                 return FALSE;
  173.             }
  174.  
  175.             $h  = "--".$this->_atc_boundary.$this->newline;
  176.             $h .= "Content-type: ".$ctype."; ";
  177.             $h .= "name=\"".$basename."\"".$this->newline;
  178.             $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
  179.             $h .= "Content-Transfer-Encoding: base64".$this->newline;
  180.            
  181.             if ($this->_attach_disp[$i] == 'inline')
  182.             {
  183.                 $cid = 'inl_'.$i;
  184.                 $cid1 = 'cid:'.$cid;
  185.                 $h .= "Content-ID: <".$cid.">".$this->newline;
  186.                 $body = $this->matched_replace_between($body, 'src=3D"', '"', $filename, $cid1, 3);
  187.                 $body = $this->matched_replace_between($body, 'href=3D"', '"', $filename, $cid1, 3);
  188.                 $body = $this->matched_replace_between($body, 'src="', '"', $filename, $cid1, 3);
  189.                 $body = $this->matched_replace_between($body, 'href="', '"', $filename, $cid1, 3);
  190.                 $body = $this->matched_replace_between($body, 'url(\'', '\');', $filename, $cid1, 3);
  191.             }
  192.  
  193.             $attachment[$z++] = $h;
  194.             $file = filesize($filename) +1;
  195.  
  196.             if ( ! $fp = fopen($filename, FOPEN_READ))
  197.             {
  198.                 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
  199.                 return FALSE;
  200.             }
  201.  
  202.             $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
  203.             fclose($fp);
  204.         }
  205.  
  206.         $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
  207.  
  208.  
  209.         if ($this->_get_protocol() == 'mail')
  210.         {
  211.             $this->_finalbody = $body;
  212.         }
  213.         else
  214.         {
  215.             $this->_finalbody = $hdr . $body;
  216.         }
  217.  
  218.         return;
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement