向此邮件表单添加附件

发布于 2024-12-27 13:00:40 字数 1570 浏览 3 评论 0原文

我有这个简单的 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 = '[email protected]';
$subject = 'Connect FORM <<';
$headers = 'From: [email protected]' . "\r\n" .
$message = $fulltext;

mail($to, $subject, $message, $headers);
echo 'file ersal shod';
?>

I have this simple php mail form. It is working and I can use it for making my forms, but I have a problem:

I want to add 2 or 3 attachments to this form. I tried a lot reading about mail at php.net, but I cannot do it by my self.

<?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 = '[email protected]';
$subject = 'Connect FORM <<';
$headers = 'From: [email protected]' . "\r\n" .
$message = $fulltext;

mail($to, $subject, $message, $headers);
echo 'file ersal shod';
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2025-01-03 13:00:40

我改进了 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("[email protected]",
    "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");
?>

I improved code from 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("[email protected]",
    "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");
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文