2011-12-12 46 views
7

Ok guys, đây là chuỗi đầu tiên của tôi và tôi đã tìm kiếm trực tuyến nhưng không có may mắn. Tôi đang thực tập và tôi đang làm việc trên một dự án yêu cầu tôi tạo trang web tạo tệp pdf khi người dùng gửi thông tin của họ. Ngay sau khi khách hàng nhấp vào nút gửi, 3 điều cần phải xảy ra:Gửi email có tệp đính kèm tệp PDF bằng cách sử dụng PHP

  1. Lưu trữ thông tin vào cơ sở dữ liệu (thực hiện),
  2. Gửi các nhân viên một email với thông tin khách hàng mới (thực hiện), và
  3. Gửi cho khách hàng email "cảm ơn bạn" với tệp đính kèm tệp pdf (không hoạt động).

Ý tôi là, khách hàng không nhận được email, nhưng khi anh/cô ấy mở file pdf, tôi nhận được thông báo lỗi sau:

"Acrobat không thể Oen 'file_name' vì nó là hoặc không phải là một loại hỗ trợ tập tin hoặc vì các tập tin đã bị hư hỏng (ví dụ, nó đã được gửi dưới dạng đính kèm email và không được giải mã một cách chính xác) ..."

Hãy ghi nhớ đây là lần fisrt của tôi thực hiện dự án tạo tệp đính kèm tệp pdf. Nếu ai đó có thể giúp tôi giải quyết vấn đề này, điều đó sẽ rất tuyệt. Cảm ơn!

Đây là mã của tôi:

<?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

tôi nhận thấy rằng bạn có application/zip. Hãy thử ứng dụng/pdf thay thế. – Raisen

+0

Tôi cũng làm như vậy, nhưng nó sẽ không hoạt động. –

+0

Chưa có suy nghĩ đúng về nó nhưng IIRC eol cho tiêu đề MIME luôn là "\ r \ n" giá trị thực trong 'PHP_EOL' thay đổi tùy theo hệ điều hành; có thể đặt một cờ lê trong các tác phẩm trong $ headers var của bạn; mặc dù theo thông số kỹ thuật nó nên được khoan dung: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html (19.3) – CD001

Trả lời

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

Đây là mã cuối cùng của tôi, và nó hoạt động hoàn hảo! –

+1

Đây là một nhận xét trễ, nhưng bạn có thể nên sử dụng 'tempnam' để tạo một tên tệp duy nhất để bạn không gặp rủi ro khi gặp sự cố với hai khách hàng nhấn tập lệnh cùng một lúc. – yakatz

0

Hãy thử điều này:

<?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

Ok DaveRandom, tôi sẽ thử ngay bây giờ! Cảm ơn bạn! –

+0

Tôi đang chờ thư đến, nhưng có vẻ như chức năng thư php mất quá nhiều thời gian để gửi tin nhắn. Email đầu tiên luôn mất quá nhiều thời gian. Sau email đầu tiên, phải mất vài giây để nhận được email. Ngay sau khi tôi nhận được email đầu tiên, tôi sẽ xác nhận nếu nó hoạt động hay không. Cảm ơn bạn! –

+1

Không có gì làm việc cho tôi, vì vậy tôi đã thay đổi mọi thứ, và tôi đã kết thúc bằng cách sử dụng lớp phpmailer. Cảm ơn tất cả sự giúp đỡ của bạn !! –

Các vấn đề liên quan