2016-07-10 6 views
0

Идея - у меня есть контактная форма, и в этой форме клиенты могут загрузить файл. Форма отправлена ​​на мое письмо с файлом.Прикрепить файл к сообщению php

У меня есть код, но он не работал. На почту приходит файл сбоя без имени, типа и размера. что я делаю неправильно?

<?php 
 
$picture = ""; 
 
if (!empty($_FILES['file']['tmp_name'])) 
 
    { 
 
    // Закачиваем файл 
 
    $path = $_FILES['file']['name']; 
 
    if (copy($_FILES['file']['tmp_name'], $path)) $picture = $path; 
 
    } 
 
$name = strip_tags(htmlspecialchars($_POST['name'])); 
 
$email_address = strip_tags(htmlspecialchars($_POST['email'])); 
 
$message = strip_tags(htmlspecialchars($_POST['message'])); 
 
    
 
// Create the email and send the message 
 
$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to. 
 
$email_subject = "Website Contact Form: $name"; 
 
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nMessage:\n$message"; 
 
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected] 
 
$headers .= "Reply-To: $email_address"; 
 

 
/*if(empty($picture)) { 
 
    \t mail($to,$email_subject,$email_body,$headers); 
 
} else {*/ 
 
    \t send_mail($to, $mail_subject, $email_body, $picture); 
 
//} 
 

 
function send_mail($to, $thema, $html, $path) 
 
    { if ($path) { 
 
    $fp = fopen($path,"rb"); 
 
    if (!$fp) 
 
    { print "Cannot open file"; 
 
     exit(); 
 
    } 
 
    $file = fread($fp, filesize($path)); 
 
    fclose($fp); 
 
    } 
 
    \t $name_file = $path; 
 
    $EOL = "\r\n"; 
 
    $boundary  = "--".md5(uniqid(time())); 
 
    $headers = "MIME-Version: 1.0;$EOL"; 
 
    $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"$EOL"; 
 
    $headers .= "From: [email protected]"; 
 
     
 
    $multipart = "--$boundary$EOL"; 
 
    $multipart .= "Content-Type: text/html; charset=windows-1251$EOL"; 
 
    $multipart .= "Content-Transfer-Encoding: base64$EOL"; 
 
    $multipart .= $EOL; 
 
    $multipart .= chunk_split(base64_encode($html)); 
 

 
    $multipart .= "$EOL--$boundary$EOL"; 
 
    $multipart .= "Content-Type: application/octet-stream; name=\"$name_file\"$EOL"; 
 
    $multipart .= "Content-Transfer-Encoding: base64$EOL"; 
 
    $multipart .= "Content-Disposition: attachment; filename=\"$name_file\"$EOL"; 
 
    $multipart .= $EOL; 
 
    $multipart .= chunk_split(base64_encode($file)); 
 

 
    $multipart .= "$EOL--$boundary--$EOL"; 
 
     
 
     if(!mail($to, $thema, $multipart, $headers)) 
 
     {return False;   
 
     } 
 
    else { 
 
    return True; 
 
    } 
 
    exit; 
 
    } 
 

 
?>

ответ

0
$email_body = "You have received a new message from your website contact form.\n\nHere are the details:\n\nName:". $name."\n\nEmail:". $email_address."\n\nMessage:\n".$message; 
Смежные вопросы