2011-12-12 159 views
7

好吧,這是我的第一個線程,我在網上搜索但沒有運氣。 我正在做一個實習,我正在開發一個項目,需要我創建一個網頁,當用戶提交他/她的信息時生成一個pdf文件。只要客戶點擊提交按鈕,3件事情需要發生:使用PHP發送PDF文件附件的電子郵件

  1. 存儲信息數據庫(完成),
  2. 發送員工一封電子郵件,新客戶的信息(已完成),以及
  3. 向客戶發送一封帶有pdf文件附件(不工作)的「thank you message」郵件。

我的意思是,客戶沒有收到一封電子郵件,但是當他/她打開PDF文件時,我收到以下錯誤信息:

「的Acrobat不能OEN‘FILE_NAME’,因爲它是要麼是不支持的文件類型,要麼是因爲文件已損壞(例如,它是作爲電子郵件附件發送的,未正確解碼)...「

請注意,這是我的最後時間做一個創建PDF文件附件的項目。如果有人能幫我解決這個問題,那就太好了。謝謝!

這裏是我的代碼:

<?php 
// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information 
     $msg = "Name: " .$_POST['name'] . "\n" 
      ."Email: " .$_POST['email'] . "\n" 
      ."Phone: " .$_POST['telephone'] . "\n" 
      ."Number Of Guests: " .$_POST['numberOfGuests'] . "\n" 
      ."Date Of Reunion: " .$_POST['date']; 
     $staffEmail = "staffinfo"; 

     mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message) 

     //once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file. 
     $pdf=new FPDF(); 
     $pdf->AddPage(); 
     $pdf->SetFont("Arial", "B", 16); 
     $pdf->Cell(40, 10, "Hello World!"); 

     // email information 
     $to = $_POST['email']; 
     $from = $staffEmail; 
     $subject = "Thank you for your business"; 
     $message = "Thank you for submitting your information!"; 

     // 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 = "yourinformation.pdf"; 

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

     // encode data (multipart mandatory) 
     $headers = "From: ".$from.$eol; 
     $headers .= "MIME-Version: 1.0".$eol; 
     $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
     $headers .= "Content-Transfer-Enconding: 7bit".$eol; 
     $headers .= "This is a MIME encoded message.".$eol.$eol; 

     // message 
     $headers .= "--".$separator.$eol; 
     $headers .= "Content-Type: text/html; charsrt=\"iso-8859-1\"".$eol; 
     $headers .= $message.$eol.$eol; 

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

     // send message 
     mail($to, $subject, $message, $headers); 

    } 
} 
?> 
+0

我注意到,你有應用程序/壓縮。嘗試應用程序/ pdf。 – Raisen

+0

我也是這樣做的,但它不起作用。 –

+0

還沒有適當考慮它,但是IIRC MIME頭文件的eol始終是「\ r \ n」,「PHP_EOL」中的實際值取決於操作系統;這可能會把一個扳手放在你的$ headers var;雖然根據規範它應該是寬容的:http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html(19.3) – CD001

回答

3
// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information 
     $msg = "Name: " .$_POST['name'] . "\n" 
      ."Email: " .$_POST['email'] . "\n" 
      ."Phone: " .$_POST['telephone'] . "\n" 
      ."Number Of Guests: " .$_POST['numberOfGuests'] . "\n" 
      ."Date Of Reunion: " .$_POST['date']; 
     $staffEmail = "staffemail"; 

     mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message) 

     //once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file. 
     // creating a pdf file 
     $pdf_filename = tempnam(sys_get_temp_dir(), "pdf"); 
     $pdf=new FPDF(); 
     $pdf->AddPage(); 
     $pdf->SetFont("Arial", "B", 16); 
     $pdf->Cell(40, 10, "Title"); 
     $pdf->Ln(); 
     $pdf->SetFont("Arial", "", 12); 
     $pdf->Cell(15, 10, "Name:"); 
     $pdf->SetFont("Arial", "I", 12); 
     $pdf->Cell(15, 10, $_POST['name']); 
     $pdf->Ln(); 
     $pdf->SetFont("Arial", "", 12); 
     $pdf->Cell(15, 10, "Email:"); 
     $pdf->SetFont("Arial", "I", 12); 
     $pdf->Cell(15, 10, $_POST['email']); 
     $pdf->Ln(); 
     $pdf->SetFont("Arial", "", 12); 
     $pdf->Cell(15, 10, "Phone:"); 
     $pdf->SetFont("Arial", "I", 12); 
     $pdf->Cell(15, 10, $_POST['telephone']); 
     $pdf->Ln(); 
     $pdf->SetFont("Arial", "", 12); 
     $pdf->Cell(40, 10, "Number of Guests:"); 
     $pdf->SetFont("Arial", "I", 12); 
     $pdf->Cell(40, 10, $_POST['numberOfGuests']); 
     $pdf->Ln(); 
     $pdf->SetFont("Arial", "", 12); 
     $pdf->Cell(40, 10, "Date Of Reunion:"); 
     $pdf->SetFont("Arial", "I", 12); 
     $pdf->Cell(40, 10, $_POST['date']); 
     // if file doesn't exists or if it is writable, create and save the file to a specific place 
     if(!file_exists($pdf_filename) || is_writable($pdf_filename)){ 
      $pdf->Output($pdf_filename, "F"); 
     } else { 
      exit("Path Not Writable"); 
     } 

     // using the phpmailer class 
     // create a new instance called $mail and use its properties and methods. 
     $mail = new PHPMailer(); 
     $staffEmail = "staffemail"; 
     $mail->From = $staffEmail; 
     $mail->FromName = "name"; 
     $mail->AddAddress($_POST['email']); 
     $mail->AddReplyTo($staffEmail, "name"); 

     $mail->AddAttachment($pdf_filename); 
     $mail->Subject = "PDF file attachment"; 

     $mail->Body = "message!"; 

     // if mail cannot be sent, diplay error message 
     //if(!$mail->Send()){ 
      //echo "<div id=\"mailerrors\">Message could not be sent</div>"; 
      //echo "<div id=\"mailerrors\">Mailer Error: " . $mail->ErrorInfo . "</div>"; 
     //} else { // else...if mail is sent, diplay sent message 
      //echo "<div id=\"mailerrors\">Message sent</div>"; 
     //} 

     // delete the temp file 
     unlink($pdf_filename); 
    } 
}  
+0

這是我的最終代碼,它完美的工作! –

+1

這是一個很晚的評論,但是您可能應該使用'tempnam'來生成一個唯一的文件名,這樣您就不會冒險讓兩個客戶一次碰到腳本。 – yakatz

0

試試這個:

<?php 

    // once there are no errors, as soon as the customer hits the submit button, 
    // it needs to send an email to the staff with the customer information 
    $msg = "Name: " .$_POST['name'] . "\n" 
     . "Email: " .$_POST['email'] . "\n" 
     . "Phone: " .$_POST['telephone'] . "\n" 
     . "Number Of Guests: " .$_POST['numberOfGuests'] . "\n" 
     . "Date Of Reunion: " .$_POST['date']; 
    $staffEmail = "staffinfo"; 
    mail($staffEmail, "You have a new customer", $msg); 

    // once the customer submits his/her information, he/she will receive a thank 
    // you message attach with a pdf file. 
    $pdf = new FPDF(); 
    $pdf->AddPage(); 
    $pdf->SetFont("Arial", "B", 16); 
    $pdf->Cell(40, 10, "Hello World!"); 

    // email information 
    $to = $_POST['email']; 
    $from = $staffEmail; 
    $subject = "Thank you for your business"; 
    $message = "Thank you for submitting your information!"; 

    // a random hash will be necessary to send mixed content 
    $separator = '-=-=-'.md5(microtime()).'-=-=-'; 

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

    // Generate headers 
    $headers = "From: $from\r\n" 
      . "MIME-Version: 1.0\r\n" 
      . "Content-Type: multipart/mixed; boundary=\"$separator\"\r\n" 
      . "X-Mailer: PHP/" . phpversion(); 

    // Generate body 
    $body = "This is a multipart message in MIME format\r\n" 
     . "--$separator\r\n" 
     . "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" 
     . "\r\n" 
     . "$message\r\n" 
     . "--$separator\r\n" 
     . "Content-Type: application/pdf\r\n" 
     . "Content-Transfer-Encoding: base64\r\n" 
     . "Content-Disposition: attachment; filename=\"$filename\"\r\n" 
     . "\r\n" 
     . chunk_split(base64_encode($pdf->Output("", "S")))."\r\n" 
     . "--$separator--"; 

    // send message 
    mail($to, $subject, $body, $headers); 
+0

好的DaveRandom,我現在就試試吧!謝謝! –

+0

我在等待郵件進來,但似乎php郵件功能花費太長時間來發送郵件。第一封電子郵件總是需要很長時間。第一封電子郵件後,需要幾秒鐘才能收到電子郵件。只要我收到第一封電子郵件,我會確認它是否有效。謝謝! –

+1

沒有爲我工作,所以我改變了一切,我最終使用phpmailer類。感謝大家的幫助! –

相關問題