2013-01-11 87 views
2

我一直在試圖讓PHP發送附帶PDF的電子郵件,我一直在尋找,並遇到這個職位Email PDF Attachment with PHP Using FPDFPHP附加PDF郵件功能

我試過解決方案,但仍然沒有運氣,有誰知道我要去哪裏錯了?我沒有收到錯誤或電子郵件?

基本上,這樣做是從表單中獲取值,將它們放入數據庫然後發送電子郵件給他們。

任何幫助將不勝感激。

require('fpdf.php'); 
include("database.php"); 
require_once('recaptchalib.php'); 
$publickey = "6LcBYNsSAAAAAKkf5LwKkrhTVTVlxmOblcOn70-r "; 
$privatekey = " 6LcBYNsSAAAAAEAa9wFZfyjInRmsWCxpj79Nvqm7"; 


// email stuff (change data below) 
$to = "[email protected]"; 
$from = "[email protected]"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>"; 


// PDF Create 
$pdf = new FPDF('P', 'pt', array(500,233)); 
$pdf->AddPage(); 
$pdf->SetFont('Arial','B',16); 
$pdf->Cell(40,10,'Hello World!'); 


// a random hash will be necessary to send mixed content 
$separator = md5(time()); 

// carriage return type (we use a PHP end of line constant) 
$eol = PHP_EOL; 

// attachment name 
$filename = "test.pdf"; 

// encode data (puts attachment in proper format) 
$pdfdoc = $pdf->Output("", "S"); 
$attachment = chunk_split(base64_encode($pdfdoc)); 

// main header 
$headers = "From: ".$from.$eol; 
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; 

// no more headers after this, we start the body! // 
$body = "--".$separator.$eol; 
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
$body .= "This is a MIME encoded message.".$eol; 

// message 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
$body .= $message.$eol; 

// attachment 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol; 
$body .= "Content-Disposition: attachment".$eol.$eol; 
$body .= $attachment.$eol; 
$body .= "--".$separator."--"; 

// form values 
$firstname = $_POST['first']; 
$lastname = $_POST['last']; 
$postcode = md5($_POST['post']); 
$email = $_POST['email']; 


if ($_POST["recaptcha_response_field"]) { 

    $resp = recaptcha_check_answer ($privatekey, 
            $_SERVER["REMOTE_ADDR"], 
            $_POST["recaptcha_challenge_field"], 
            $_POST["recaptcha_response_field"]); 
    if ($resp->is_valid) { 

          $check = mysql_query("SELECT * FROM voucher WHERE first='$firstname' AND last='$lastname'"); 
          if(mysql_num_rows($check) != 0) 
          { 
           echo "<p>Voucher already in use.</p>"; 
          } 
          else 
          { 
           $insert = 'INSERT into voucher(first, last, postcode, email) VALUES ("'.$firstname.'","'.$lastname.'","'.$postcode.'","'.$email.'")'; 
           mysql_query($insert); 
           // send message 
           mail($to, $subject, $message, $headers) or die("Mail Error");     
           echo "<p>Voucher has been emailed, we looked foward to seeing you soon</p>"; 
          } 
} 
else 
{ 
    echo "The anti spam code you entered is not correct."; 
} 

}

+0

我會首先執行您的反垃圾郵件檢查 - 否則服務器正在爲可能是垃圾郵件的事情做很多工作。 – Kaiesh

+0

爲什麼人們不斷嘗試使用PHP的'mail()'函數來創建複雜的郵件?對於這樣的事情,**使用[像phpMailer這樣體面的郵件類](http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail/12302354#12302354)爲自己節省數小時的**。 – SDC

回答

4

我會將您的反垃圾郵件檢查移到頂端,但我得到了下面的代碼來工作。我將您的EOL標籤更改爲「\ r \ n」,並在標頭的末尾添加了另一個$ eol。我還在mail()調用中將$ message更改爲$ body,因爲所有準備工作都是爲$ body完成的,但未使用。

// a random hash will be necessary to send mixed content 
$separator = md5(time()); 

// carriage return type (we use a PHP end of line constant) 
$eol = "\r\n"; 

// attachment name 
$filename = "test.txt"; 

// encode data (puts attachment in proper format) 
$attachment = chunk_split(base64_encode("I am text file")); 

// main header 
$headers = "From: ".$from.$eol; 
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol; 

// no more headers after this, we start the body! // 
$body = "--".$separator.$eol; 
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
$body .= "This is a MIME encoded message.".$eol; 

// message 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol; 
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol; 
$body .= $message.$eol; 

// attachment 
$body .= "--".$separator.$eol; 
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol; 
$body .= "Content-Disposition: attachment".$eol.$eol; 
$body .= $attachment.$eol; 
$body .= "--".$separator."--"; 

// form values 
$firstname = $_POST['first']; 
$lastname = $_POST['last']; 
$postcode = md5($_POST['post']); 
$email = $_POST['email']; 


echo mail($to, $subject, $body, $headers) or die("Mail Error");     
echo "<p>Voucher has been emailed, we looked foward to seeing you soon</p>\n"; 
+0

非常感謝你:) – Brent

+0

替換 $ attachment = chunk_split(base64_encode(「I am text文件」)); 與 chunk_split(base64_encode(file_get_contents($ filename))); 更好的「'複製和粘貼'性」。 –

1

有這麼多的可能的突破點在此代碼,這是不可能告訴什麼不順心。你需要自己做一些調試,例如。使用測試輸出來查看腳本到底有多遠。另外請確保您激活錯誤報告。

發送附件可能會使用SwiftMailer library.

+0

我已經盡我所知地嘗試過測試,並且一切看起來都很好。我會看看謝謝 – Brent

2

我有一個郵件類編寫一個簡單的方法。附件代碼是這樣的:

$body .= "--".$separator."\r\n"; 
$body .= "Content-Disposition: attachment; filename=\"".$filename."\";\r\n"; 
$body .= "Content-Length: ".$filesize.";\r\n"; 
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; 
$body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
$body .= $attachment."\r\n"; 
$body .= "--".$separator."--"; 

你看到一些差別:我Content-Length,2X $filename\r\n爲EOL。

我希望這會有所幫助。

+0

當我嘗試這個我得到這個錯誤(致命的錯誤:當不在對象上下文中使用$這個) – Brent

+0

好的抱歉..這是來自我的課程。用'$ body'替換'$ this - > _ body'並用'filesize()'得到'$ filesize'。 – bitWorking

+0

找不到$ this->,但感謝您的輸入。 – Brent