PHP 邮件表单 - 不适用于大型附件

PHP mail form - doesnt work for large attachments

提问人:Jabbamonkey 提问时间:9/13/2023 最后编辑:Jabbamonkey 更新时间:9/13/2023 访问量:35

问:

我使用 PHP 和 mail() 函数创建了一个表单来发送附件。它适用于 1MB 以下的文件,并且没有上传附件。但是,它不适用于较大的文件。

我检查了stackoverflow,每个人都建议检查我的PHP.ini文件。我对我的 php.ini 进行了以下更改(并且为了让它工作而进行了过度补偿)......

memory_limit = 250M  
max_input_time = 300  
max_execution_time = 300
post_max_size = 30M  
upload_max_filesize = 30M

以下是我如何构建标题和正文......

    $filetmpname = $_FILES['fileupload']['tmp_name']; 
    $filename    = $_FILES['fileupload']['name'];
    $fileupload = $serverpath . basename($filename);
    $filesize    = $_FILES['fileupload']['size']; 
    $filetype    = $_FILES['fileupload']['type']; 
    $fileerror   = $_FILES['fileupload']['error'];

    $boundary = md5("random");

    
    $headers = "MIME-Version: 1.0\r\n" .
        "From: ". $theiremail."\r\n" . 
        "Cc: [email protected]\r\n" . 
        "Reply-To: ".$theiremail."\r\n" .
        "Content-Type: multipart/mixed;" .  
        "boundary = $boundary\r\n";


        //read from the uploaded file & base64_encode content
        $handle = fopen($filetmpname, "r"); // set the file handle only for reading the file
        $datacontent = fread($handle, $filesize); // reading the file
        fclose($handle);                 // close upon completion
        $data = chunk_split(base64_encode($datacontent));

        $body = "--$boundary\r\n" . 
        "Content-Type: text/html; charset=ISO-8859-1\r\n" .
        "Content-Transfer-Encoding: quoted-printable\n\n" .  
        $textcontent . "\n\n";
        // "Content-Transfer-Encoding: base64\r\n\r\n" . 
        // chunk_split(base64_encode($textcontent));
        
        $body .= "--{$boundary}\r\n" .
        "Content-Type: $filetype; name=".$filename."\r\n" . 
        "Content-Disposition: attachment; filename=".$filename."\r\n" . 
        "Content-Transfer-Encoding: base64\r\n" . 
        "X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n" . 
        $data."\n\n"; // Attaching the encoded file with email

此外,当我运行 mail() 时,电子邮件没有通过,但没有出现错误(下面没有$errorMessage输出)......注意:电子邮件会以较小的文件发送。

$ok = mail($toaddress, $subject, $body, $headers);  
if (!$ok) {
    $errorMessage = error_get_last()['message'];
    echo "Message Failed. Error:";
    echo $errorMessage;
} else {
    echo "Message Sent";
}

当我var_dump($_FILES)时,我得到一个数组,因此附件被识别...

array(1) {
  ["fileupload"]=>
  array(5) {
    ["name"]=>
    string(46) "mapimage1p2.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(14) "/tmp/phpVwAQJQ"
    ["error"]=>
    int(0)
    ["size"]=>
    int(1331087)
  }
}

再来一次。。。这适用于小型附件,但不适用于大型附件。有人知道如何解决这个问题吗?或者问我的虚拟主机什么?

PHP 表单 附件

评论

1赞 Barmar 9/13/2023
请在邮件中展示您是如何创建附件的。最好使用像 PHPMailer 这样的库,而不是滚动自己的附件代码。
1赞 Barmar 9/13/2023
许多邮件系统对邮件大小有限制。您的服务器有什么限制?
0赞 Jabbamonkey 9/13/2023
上面发布了一些澄清...至于服务器限制,我需要检查一下。但是,我只测试了 1-2MB 的文件(这应该没有问题)。任何低于 1MB 的内容都可以正常工作。
0赞 Barmar 9/13/2023
这听起来确实像邮件服务器中有 1 MB 的限制。
2赞 Barmar 9/13/2023
您是否检查过邮件服务器日志?

答: 暂无答案