php Функция отправки письма с файлом

// Функция отправки письма function XMail($from, $to, $subj, $text, $files){ //$subj=substr(htmlspecialchars(trim($subj)), 0, 1000); //$subj=mb_convert_encoding($subj, "UTF-8", "CP1251"); //$subj=base64_encode($subj); //$subj="=?UTF-8?Q?".imap_8bit($subj)."?="; $subj="=?UTF-8?B?".base64_encode($subj)."?="; $un = strtoupper(uniqid(time())); $head = 'From: '.$from."\n"; //$head .='Reply-To: '.$from."\r\n"; //$head .= 'Bcc: ' .$from. "\r\n"; //$head .= "To: $to\n"; $head .= "Subject: $subj\n"; //$head .= "Subject: =?UTF-8?Q?".imap_8bit($subj)."?="; $head .= "X-Mailer: PHPMail Tool\n"; //$head .= "Reply-To: $from\n"; $head .= "Mime-Version: 1.0\n"; $head .= "Content-Type:multipart/mixed;"; $head .= "boundary=\"----------".$un."\"\n\n"; $zag = "------------".$un."\nContent-Type:text/html;charset=utf-8\n"; //utf-8 windows-1251 //$zag .= "\nContent-type:text/plain;charset=utf-8\r\n"; $zag .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n"; if(count($files)>0){ foreach($files as $key=>$value){ if(file_exists($value)){ if(!is_dir($value)){ $filename = $value; $f = $value['num']; $f = fopen($value,"rb"); $zag .= "------------".$un."\n"; $zag .= "Content-Type: application/octet-stream;"; $zag .= "name=\"".basename($filename)."\"\n"; $zag .= "Content-Transfer-Encoding:base64\n"; $zag .= "Content-Disposition:attachment;"; $zag .= "filename=\"".basename($filename)."\"\n\n"; $zag .= chunk_split(base64_encode(fread($f,filesize($filename))))."\n"; } } } } //file_put_contents('./test.txt', print_r(["$to", "$subj", $zag, $head], true)); if (!@mail("$to", "$subj", $zag, $head)) return 0; else return 1; } // Отправка одного письма function mail_go($mail_to, $title, $text){ //return $mail_to.' '.$title.' '.$text; return XMail('"USER"<user@site.com>', $mail_to, $title, $text, []); }