2012-10-15 75 views
0

我嘗試使用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 

有人可以提供一些關於如何解決這個問題的更多信息?

回答

0

我認爲最簡單的解決方案是使用PHPMailer或SwiftMailer這樣的腳本。這是大多數人的做法,並且避免了使用PHP發送郵件的困難。試圖在PHP中使用mail()函數而不是專家是一個殺手!

0

您嘗試過的附件大小是多少?也許你應該增加上傳文件大小限制,請參閱http://drupal.org/node/97193

+0

我懷疑這個尺寸是個問題......它只有13KB – Michiel

2

這可能是由於您嘗試上傳的文件超過了大小。在您的服務器配置中增加'Max_upload_filesize'。

+0

這個文件絕對不是太大。它只有13 KB ... – Michiel