PHP电子邮件脚本,用于发送带有附件的电子邮件而不发送视频文件

我从codexworld找到了一个脚本,该脚本使用PHP发送带有附件的电子邮件。我确实做了一些稍微的编辑,因为我需要添加一些输入,但是它有一个例外,它不会发送视频文件。我将发送图片和文档文件,但不发送视频。我一直收到状态消息:“您的联系请求提交失败,请重试。”

任何人都可以看一下这段代码,然后告诉我为什么会发生这种情况以及如何解决它吗?

    <?php
$postData = $uploadedFile = $statusMsg = '';
$msgClass = 'errordiv';
if(isset($_POST['submit'])){
    // Get the submitted form data
    $postData = $_POST;
    $email = $_POST['email'];
    $name = $_POST['name'];
    $category = $_POST['category'];
    $location=$_POST['location'];
    $URL = $_POST['URL'];
    $message = $_POST['message'];
    $hauntname = $_POST['hauntname'];

// Check whether submitted data is not empty
if(!empty($email) && !empty($name) && !empty($message)){

    // Validate email
    if(filter_var($email,FILTER_VALIDATE_EMAIL) === false){
        $statusMsg = 'Please enter your valid email.';
    }else{
        $uploadStatus = 1;

        // Upload attachment file
        if(!empty($_FILES["attachment"]["name"])){

            // File path config
            $targetDir = "uploads/";
            $fileName = basename($_FILES["attachment"]["name"]);
            $targetFilePath = $targetDir . $fileName;
            $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

             // Allow certain file formats
            $allowTypes = array('m4p','mp4','jpg','png','jpeg','mov');
            if(in_array($fileType,$allowTypes)){
                // Upload file to the server
                if(move_uploaded_file($_FILES["attachment"]["tmp_name"],$targetFilePath)){
                    $uploadedFile = $targetFilePath;
                }else{
                    $uploadStatus = 0;
                    $statusMsg = "Sorry,there was an error uploading your file.\r\n";
                }
            }else{
                $uploadStatus = 0;
                $statusMsg = "Sorry,only image or video files are allowed to be uploaded.\r\n";
            }

         }

        if($uploadStatus == 1){

            // Recipient
            $toEmail = 'submission@hyhpawardshow.com';

            // Sender
            $from = $email;
            $fromName = $name;

            // Subject
            $emailSubject = $category.' submission from '.$name;

            // Message 
            $htmlContent = '<h2>Submission</h2>
                <p><b>Name:</b> '.$name.'</p>
                <p><b>Haunt Name:</b> '.$hauntname.'</p>
                <p><b>Location:</b> '.$location.'</p>
                <p><b>Email:</b> '.$email.'</p>
                <p><b>Category:</b> '.$category.'</p>
                <p><b>URL:</b> '.$URL.'</p>
                <p><b>Message:</b><br/>'.$message.'</p>';

            // Header for sender info
            $headers = "From: $fromName"." <".$from.">";

            if(!empty($uploadedFile) && file_exists($uploadedFile)){

                // 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/html; charset=\"UTF-8\"\n" .
                "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n"; 

                // Preparing attachment
                if(is_file($uploadedFile)){
                    $message .= "--{$mime_boundary}\n";
                    $fp =    @fopen($uploadedFile,"rb");
                    $data =  @fread($fp,filesize($uploadedFile));
                    @fclose($fp);
                    $data = chunk_split(base64_encode($data));
                    $message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" . 
                    "Content-Description: ".basename($uploadedFile)."\n" .
                    "Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" . 
                    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
                }

                $message .= "--{$mime_boundary}--";
                $returnpath = "-f" . $email;

                // Send email
                $mail = mail($toEmail,$emailSubject,$message,$headers,$returnpath);

                // Delete attachment file from the server
                @unlink($uploadedFile);
            }else{
                 // Set content-type header for sending HTML email
                $headers .= "\r\n". "MIME-Version: 1.0";
                $headers .= "\r\n". "Content-type:text/html;charset=UTF-8";

                // Send email
                $mail = mail($toEmail,$htmlContent,$headers); 
            }

            // If mail sent
            if($mail){
                $statusMsg = "Your contact request has been submitted successfully !\r\n";
                $msgClass = 'succdiv';

                $postData = '';
            }else{
                $statusMsg = "Your contact request submission failed,please try again.\r\n";
            }
        }
    }
}else{
    $statusMsg = 'Please fill all the fields.';
}
}

    ?>
qiche520 回答:PHP电子邮件脚本,用于发送带有附件的电子邮件而不发送视频文件

因此,事实证明,这根本不是我的代码,而是邮件服务器。最大文件大小被弄乱了,这就是导致它失败的原因。我目前正在与托管技术支持合作以解决此问题。

谢谢大家的帮助。我真的很感激。

本文链接:https://www.f2er.com/3087918.html

大家都在问