【问题标题】:Adding an attachment to this mail form向此邮件表单添加附件
【发布时间】:2012-01-17 09:11:14
【问题描述】:

我有这个简单的 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';
?>

【问题讨论】:

    标签: email attachment php


    【解决方案1】:

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

    【讨论】:

    • 在你的代码中使用multi_attach_mail insteed mail 函数类似... $message = $fulltext; multi_attach_mail($to, $subject, $message, array('/path/to/file1.ext','/another/path/file2.ext'), 'some@email.com'); echo 'file ersal shod';
    猜你喜欢
    • 2013-11-19
    • 1970-01-01
    • 2012-07-10
    • 2021-12-23
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多