在此邮件表单中添加附件

时间:2012-01-17 09:11:15

标签: email attachment php

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

我想在此表单中添加2个或3个附件。我在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';
?>

1 个答案:

答案 0 :(得分:0)

我改进了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");
?>