PHP表单发送到邮件与文件附件

时间:2015-01-15 00:55:27

标签: php forms email attachment

我有一个PHP表单的脚本发送到带有文件附件的Mail,但它没有一个函数来有效地调整文件大小和文件扩展名。

下面的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做。

1 个答案:

答案 0 :(得分:1)

您将使用类似的内容来验证文件扩展名:

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); 
    }
//[...]