正在向此邮件表单添加附件


Adding an attachment to this mail form

我有一个简单的php邮件表单。它正在工作,我可以用它来制作我的表单,但我遇到了一个问题:

我想在这个表格上添加两三个附件。我在php.net上读了很多关于邮件的文章,但我自己做不到。

<?php
$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$fax=$_POST['fax'];
$mobile=$_POST['mobile'];
$subject=$_POST['subject'];
$website=$_POST['website'];
$message=$_POST['message'];
$fulltext = "
______________________________________________
| 
| This Is $name Information: 
|______________________________________________
| Name : $name
|______________________________________________
| E-Mail : $email
|______________________________________________
| Address : $address
|______________________________________________
| Phone : $phone
|______________________________________________
| FAX : $fax
|______________________________________________
| Mobile : $mobile
|______________________________________________
| Subject : $subject
|______________________________________________
| Website : $website
|______________________________________________
| Message : $message
|______________________________________________ 
";
$to = 'support@site.com';
$subject = 'Connect FORM <<';
$headers = 'From: contactform@site.com' . "'r'n" .
$message = $fulltext;
mail($to, $subject, $message, $headers);
echo 'file ersal shod';
?>

我改进了http://ru.php.net/manual/ru/function.mail.php#105661:

<?php
function multi_attach_mail($to, $subject, $message, $files, $sendermail){
    // email fields: to, from, subject, and so on
    $from = "Files attach <".$sendermail.">"; 
    //$subject = date("d.M H:i")." F=".count($files);
    $message .= "'n".count($files)." attachments";
    $headers = "From: $from";
    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
    // headers for attachment 
    $headers .= "'nMIME-Version: 1.0'n" . "Content-Type: multipart/mixed;'n" . " boundary='"{$mime_boundary}'""; 
    // multipart boundary 
    $message = "--{$mime_boundary}'n" . "Content-Type: text/plain; charset='"iso-8859-1'"'n" .
    "Content-Transfer-Encoding: 7bit'n'n" . $message . "'n'n"; 
    // preparing attachments
    for($i=0;$i<count($files);$i++){
        if(is_file($files[$i])){
            $message .= "--{$mime_boundary}'n";
            $fp =    @fopen($files[$i],"rb");
        $data =    @fread($fp,filesize($files[$i]));
                    @fclose($fp);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: application/octet-stream; name='"".basename($files[$i])."'"'n" . 
            "Content-Description: ".basename($files[$i])."'n" .
            "Content-Disposition: attachment;'n" . " filename='"".basename($files[$i])."'"; size=".filesize($files[$i]).";'n" . 
            "Content-Transfer-Encoding: base64'n'n" . $data . "'n'n";
            }
        }
    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $sendermail;
    $ok = @mail($to, $subject, $message, $headers, $returnpath); 
    if($ok){ return $i; } else { return 0; }
    }
multi_attach_mail("to@somebody.dom",
    "Subject of mail" ,
    "Hello world!!!",
    array($_SERVER["DOCUMENT_ROOT"] . "/first.file", // one file in root of your site
        $_SERVER["DOCUMENT_ROOT"] . "/second.file" // another one
    ),
    "from@someone");
?>