dongshai2022 2016-11-22 16:05 采纳率: 100%
浏览 39

无法添加附件

Good afternoon,

I can't send attachments within my emails send from website, but it does do see the attachment. How can I add the attachment to the emails send. I do see text like:

--451b4ac97a84a2205e1d116ef096f765 Content-Type:"image/jpeg; name="testbeeld.jpg" Content-Disposition: attachment; filename="testbeeld.jpg" Content-Transfer-Encoding: base64 X-Attachment-Id: 66045

in my body. Thanks for taking a look into it.

if($_POST && isset($_FILES['File_upload']))
{
$recipient_email    = $_POST['email2']; //recepient
$from_email         = $_POST['email']; //from email using site domain.
$subject            = $_POST['title']; //email subject line

$sender_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
$sender_message = filter_var($_POST['message'], FILTER_SANITIZE_STRING); //capture message
$attachments = $_FILES['File_upload'];

$file_count = count($attachments['name']); //count total files attached
$boundary = md5("keukenaanbod.nl");

if($file_count > 0){ //if attachment exists
    //header
    $headers = 'MIME-Version: 1.0
'.
               'From: info@keukenaanbod.nl' . "
".
               'Reply-To: ' . $sender_email . '' . "
" .
               'Cc: ' . $sender_email . '' . "
" .
               'X-Mailer: PHP/' . phpversion().
               'Content-Type: multipart/mixed; boundary = $boundary

';


    //message text
    $body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:
\"" . $sender_message . "\"


";

    //attachments
    for ($x = 0; $x < $file_count; $x++){      
        if(!empty($attachments['name'][$x])){

            if($attachments['error'][$x]>0) //exit script and output error if we encounter any
            {
                $mymsg = array(
                1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
                2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
                3=>"The uploaded file was only partially uploaded",
                4=>"No file was uploaded",
                6=>"Missing a temporary folder" );
                die($mymsg[$attachments['error'][$x]]);
            }

            //get file info
            $file_name = $attachments['name'][$x];
            $file_size = $attachments['size'][$x];
            $file_type = $attachments['type'][$x];

            //read file
            $handle = fopen($attachments['tmp_name'][$x], "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

            $body .= "--$boundary
";
            $body .="Content-Type: $file_type; name=" . $file_name . "
";
            $body .="Content-Disposition: attachment; filename=" . $file_name . "
";
            $body .="Content-Transfer-Encoding: base64
";
            $body .="X-Attachment-Id: ".rand(1000,99999)."

";
            $body .= $encoded_content;
        }
    }

}else{ //send plain email otherwise
           $headers =   "MIME-Version: 1.0" . "
".
                        "Content-type:text/html;charset=UTF-8" . "
".
                        'From: info@keukenaanbod.nl' . "
".
                        'Reply-To: ' . $sender_email . '' . "
" .
                        'Cc: ' . $sender_email . '' . "
" .
                        'X-Mailer: PHP/' . phpversion();
           $body = $sender_message;
}

 $sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{      
    header('Location: /verzenden-gelukt?id='. $id .'');
}else{
    die('Email kon helaas niet verzonden worden, u dient direct uit te zoeken wat er gaande is!');  
}
}


}
  • 写回答

1条回答 默认 最新

  • dongmin4052 2016-11-23 13:20
    关注

    I solved my problem by using the following code, this will output a file as attachment (multiple files are allowed) and placed plain text above the added attachment if set.

    Hope someone has a good use for it.

    if($_POST && isset($_FILES['File_upload']))
    {
    $recipient_email    = $_POST['email2']; //recepient
    $from_email         = $_POST['email']; //from email using site domain.
    $subject            = $_POST['title']; //email subject line
    
    $sender_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
    $sender_message = filter_var($_POST['message'], FILTER_SANITIZE_STRING); //capture message
    $attachments = $_FILES['File_upload'];
    
    $file_count = count($attachments['name']); //count total files attached
    $boundary = md5(time());
    $eol = "
    ";
    
    if($file_count > 0){ //if attachment exists
        //header
        $headers = 'From: email@domain.nl<email@domain.nl>' . $eol; 
        $headers .= 'Reply-To: '. $sender_email . '<' . $sender_email . '>' . $eol;
        $headers .= 'Cc: ' . $sender_email . $eol;
        $headers .= "Message-ID: <" . time() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">" . $eol; 
        $headers .= "X-Mailer: PHP v" . phpversion() . $eol;
        $headers .= 'MIME-Version: 1.0' . $eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"" . $eol; 
    
    
        //message text
        //$body = "--" . $boundary . $eol;
        //$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
        //$body .= "Content-Transfer-Encoding: 8bit" . $eol;
        //$body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:". $eol . $sender_message . $eol . $eol;
    
        //attachments
        for ($x = 0; $x < $file_count; $x++){      
            if(!empty($attachments['name'][$x])){
    
                //get file info
                $file_name = $attachments['name'][$x];
                $file_size = $attachments['size'][$x];
                $file_type = $attachments['type'][$x];
    
                //read file
                $handle = fopen($attachments['tmp_name'][$x], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
    
                $body = "--". $boundary . $eol;
                $body .= "Content-type:text/plain; charset=iso-8859-1" . $eol;
                $body .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
                $body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:". $eol . $sender_message . $eol . $eol;
                $body .= "--" . $boundary . $eol;
                $body .= "Content-Type: " . $file_type. "; name=\"" . $file_name . "\"" . $eol;
                $body .= "Content-Transfer-Encoding: base64" . $eol; 
                $body .= "Content-Disposition: attachment; filename=\"" . $file_name . "\"" . $eol . $eol;
                $body .= $encoded_content . $eol . $eol;
            }
        }
    
    }else{ //send plain email otherwise
               $headers =   "MIME-Version: 1.0" . "
    ".
                            "Content-type:text/html;charset=UTF-8" . "
    ".
                            'From: mail@domain.nl' . "
    ".
                            'Reply-To: ' . $sender_email . '' . "
    " .
                            'Cc: ' . $sender_email . '' . "
    " .
                            'X-Mailer: PHP/' . phpversion();
               $body = $sender_message;
    }
    
     $sentMail = @mail($recipient_email, $subject, $body, $headers);
    if($sentMail) //output success or failure messages
    {      
        header('Location: /verzenden-gelukt?id='. $id .'');
    }else{
        die('Email kon helaas niet verzonden worden, u dient direct uit te zoeken wat er gaande is!');  
    }
    }
    
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考