PHP 表单连同文件附件一起发送到 Mail

PHP form send to Mail with file attachment

我有一个用于 PHP 表单发送到带有文件附件的邮件的脚本,但它没有验证文件大小和文件扩展名的功能。

php 下面的代码

### Attachment Preparation ###

$file_attached = false;
if(isset($_FILES['file_attach'])) //check uploaded file
{
    //get file details we need
    $file_tmp_name    = $_FILES['file_attach']['tmp_name'];
    $file_name        = $_FILES['file_attach']['name'];
    $file_size        = $_FILES['file_attach']['size'];
    $file_type        = $_FILES['file_attach']['type'];
    $file_error       = $_FILES['file_attach']['error'];

    //exit script and output error if we encounter any
    if($file_error>0)
    {
        $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" ); 

        $output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
        die($output); 
    }

    //read from the uploaded file & base64_encode content for the mail
    $handle = fopen($file_tmp_name, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $encoded_content = chunk_split(base64_encode($content));
    //now we know we have the file for attachment, set $file_attached to true
    $file_attached = true;
}

if($file_attached) //continue if we have the file
{
    # Mail headers should work with most clients
    $headers = "MIME-Version: 1.0\r\n";
    $headers = "X-Mailer: PHP/" . phpversion()."\r\n";
    $headers .= "From: ".$from_email."\r\n";
    $headers .= "Subject: ".$subject."\r\n";
    $headers .= "Reply-To: ".$user_email."" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";

    $headers .= "--".md5('boundary1')."\r\n";
    $headers .= "Content-Type: multipart/alternative;  boundary=".md5('boundary2')."\r\n\r\n";

    $headers .= "--".md5('boundary2')."\r\n";
    $headers .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
    $headers .= $message_body."\r\n\r\n";

这是 php 表单代码

<label>
    <span>Attachment</span>
    <input type="file" name="file_attach" class="input-field" />
</label>
<label>
    <span>&nbsp;</span><input type="submit" id="submit_btn" value="Submit" />
</label>

我不想使用 jQuery 我想使用 php。

您将使用类似这样的方法来验证文件扩展名:

if($_FILES["file_attach"]["type"] == "image/jpeg") { //...

将 "image/jpeg" 替换为您希望它具有的实际文件类型的任何 $_FILE['file_attach']['type'] 值。

至于验证大小,您将执行如下操作:

if($_FILE["file_attach"]["size"] > 1000000) { //...

最后,这可能是将其应用到现有脚本中的最佳方式:

//[...]


$file_error       = $_FILES['file_attach']['error'];


if($_FILES["file"]["type"] != "image/jpeg"){
    $file_error = 7;
}elseif($_FILES["file_attach"]["size"] > 1000000) { // = 1MB
    $file_error = 8;
}


    //exit script and output error if we encounter any
    if($file_error>0)
    {
        $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" 
        7=>"We do not accept files of that type.",
        8=>"Files cannot be larger than 1MB" );

        $output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
        die($output); 
    }
//[...]