面临的问题:尝试使用 PHP 发送电子邮件,最多 20 MB 数据作为电子邮件附件,所有文件都是 .pdf 文件。我可以成功发送最多 5 MB 的电子邮件,但如果尝试发送超过 5 MB 的数据,我的收件箱中从未收到电子邮件,PHP 也没有显示错误。我在许多论坛上搜索过这个问题,并尝试了他们提供的所有解决方案。就像 php.ini 文件中的设置一样。
php.ini 设置:
memory_limit = 256M
max_execution_time = 60
max_file_uploads = 25
post_max_size = 25M
upload_max_filesize = 10M
我正在使用的代码:
$to = $settings->factoring_email;
$from = ucfirst($settings->company_name)."<".$settings->company_billing_email.">";
$subject = ucfirst($settings->company_name).' Invoice Manifest '.$post['inv_date'];
$message = "Please see attached documents.";
$headers = "From: $from";
// 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 = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
$count = count($files);
for($x=0;$x<$count;$x++){
if(stristr($files[$x],'manifest')) {
$path = BASE_PATH.DS.'docs'.DS.'tmp'.DS.$files[$x];
$name = $files[$x];
}
else {
$path = BASE_PATH.DS.'docs'.DS.'load_info'.DS.$files[$x].'.pdf';
$name = $files[$x].'.pdf';
}
if(file_exists($path)) {
$file = fopen($path,"rb");
$data = fread($file,filesize($path));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
unset($file, $data);
}
}
// send
$ok = mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}