我嘗試使用PHP在我的網站上發送自動創建的電子郵件。經過一番努力,我結束了這個腳本,但由於某種原因,我得到一個錯誤,我不明白爲什麼。使用PHP發送附件的問題
function sendMail($_POST) {
$fileatt = "http://www.mysite.com/contract.pdf";
$fileatt_type = "application/pdf";
$fileatt_name = "contract.pdf";
$email_from = "[email protected]";
$email_subject = "Your Contract";
$email_message = "Thanks for your booking! Here is your contract";
$email_to = $_POST['mail']; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'r');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "You file has been sent to the email address you specified";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
}
並且該腳本確實發送了電子郵件幷包含附件,但附件的大小爲0kb。這是有道理的,如果我看到我的錯誤:
Warning: filesize(): stat failed for http://www.mysite.com/test.pdf in /customers/9/7/5/mysite.com/functions.php on line 712
Warning: fread(): Length parameter must be greater than 0 in /customers/9/7/5/mysite.com/functions.php on line 712
有人可以提供一些關於如何解決這個問題的更多信息?
我懷疑這個尺寸是個問題......它只有13KB – Michiel