Отправка письма с вложением ПХП

Discussion in 'PHP' started by e|\|ot, 9 Jun 2013.

  1. e|\|ot

    e|\|ot New Member

    Joined:
    26 Sep 2012
    Messages:
    0
    Likes Received:
    0
    Reputations:
    0
    Решил скрипт отправки письма переделать чтоб аттач слал, но возникли проблемы, приходит путое пимо на маил. Вот часть кода:

    PHP:
    <?php
    function quoted_printable_encode $string ) {
       
    $string preg_replace_callback (
       
    '/[^\x21-\x3C\x3E-\x7E\x09\x20]/',
       
    'quoted_printable_encode_character',
       
    $string
       
    );
       
    $newline "=\r\n"// '=' + CRLF (rule #4)
       
    $string preg_replace '/(.{73}[^=]{0,3})/''$1'.$newline$string);
       return 
    $string;
    }

    function 
    mime_header_encode($str$data_charset$send_charset) {
      if(
    $data_charset != $send_charset) {
        
    $str iconv($data_charset$send_charset$str);
      }
      return 
    '=?' $send_charset '?B?' base64_encode($str) . '?=';
    }
     
    {
     
    $data_charset='utf-8';  
     
    $send_charset='windows-1251';


    $_POST['to'] = stripslashes($_POST['to']);
    $_POST['msg'] = stripslashes($_POST['msg']);
    $_POST['from'] = stripslashes($_POST['from']);
    $_POST['fromname'] = stripslashes($_POST['fromname']);
    $_POST['subject'] = stripslashes($_POST['subject']);
    $_POST['tipe'] = stripslashes($_POST['tipe']);

    if(
    $_POST['to'] && $_POST['msg'] && $_POST['from'] && $_POST['tipe'])
    {

      
    $subject mime_header_encode($_POST['subject'], $data_charset$send_charset);
      
    $from =  mime_header_encode($_POST['fromname'], $data_charset$send_charset)
                         .
    ' <' $_POST['from'] . '>';
      
    $body iconv($data_charset$send_charset$_POST['msg']);                     
      
    $headers "MIME-Version: 1.0\r\n";
      
    $headers .= "Content-type: text/".$_POST['tipe']."; charset=windows-1251\r\n";
      
    $headers .= "From: ".$from."\n";

      
      
    $boundary "--".md5(uniqid(time())); 
      
    $headers .="Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
      
    $multipart "--".$boundary."\r\n";
      
    $multipart .= "Content-type: text/$tipe; charset=\"windows-1251\"\r\n";
      
    $multipart .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
      
    $body quoted_printable_encode$body )."\r\n\r\n";
      
    $multipart .= $body;
      
      {
      
    $d opendir('attach'); 
      
    $filelist = array(); 
      while (
    $filename=readdir($d)) { 
      if (
    $filename!='.' && $filename!='..') { 
      
    $filelist[]=$filename;} 
      } 
      
    closedir ($d);
      }
      
      
    $file '';
      
    $count count$filelist );
      if ( 
    $count ) {
        for ( 
    $i 0$i $count$i++ ) {
          
    $fp fopen($filelist[$i], "r");
          if ( 
    $fp ) {
            
    $content fread($fpfilesize($filelist[$i]));
            
    fclose($fp);
            
    $file .= "--".$boundary."\r\n";
            
    $file .= "Content-Type: application/octet-stream\r\n";
            
    $file .= "Content-Transfer-Encoding: base64\r\n";
            
    $file .= "Content-Disposition: attachment; filename=\"".$filename[$i]."\"\r\n\r\n";
            
    $file .= chunk_split(base64_encode($content))."\r\n";      
          }
        }
      }
      
    $multipart .= $file."--".$boundary."--\r\n";

      
    $to=$_POST['to'];
     if (
    mail($to$subject$multipart$headers)) 
    {
    echo (
    'ok');
    }
    else
    echo (
    'error send');
    }
    else
    echo(
    'error data');
    }
    ?>

    Нужно чтобы слало из указанной папки все файлы которые в ней.
    Заранее спасибо.
     
  2. mustedlife

    mustedlife New Member

    Joined:
    31 Jan 2013
    Messages:
    11
    Likes Received:
    3
    Reputations:
    0
    1 person likes this.
  3. e|\|ot

    e|\|ot New Member

    Joined:
    26 Sep 2012
    Messages:
    0
    Likes Received:
    0
    Reputations:
    0
    Все норм работает, были проблемы с хостингом и с пост запросами.