2015-01-19 1 views
1

FPDF를 사용하여 PDF 파일을 만들고 PHPMailer를 사용하여 전자 메일로 보내려고합니다. 그것은 파일을 보냈지 만 몇 가지 이유 때문에 이메일에서나 Adobe Reader를 사용하여 파일을 열 수 없습니다 (파일이 손상된 것처럼 보입니다).FPDF를 사용하여 이메일을 보낸 후 FPDF PDF 파일을 열 수 없습니다.

이 내가 $ PDF-를 사용할 때이> 나에게 문제가 FPDF 측에 안 나에게 힌트 출력() 출력을 제공 않습니다 내 코드

require('fpdf.php'); 
require 'PHPMailerAutoload.php'; 
header("Access-Control-Allow-Origin: *"); 
$pdf = new FPDF(); 
$pdf->AddPage(); 

$pdf->SetFont('Arial','UB',28); 
$pdf->Cell(0,10,"My Profile",0,1,'C'); 
$pdf->Ln(); 

$mail = new PHPMailer(); 
$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '[email protected]';     // SMTP username 
$mail->Password = 'password';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587; 
// 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; 

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

$pdfdoc = $pdf->Output('','S'); 
$attachment = chunk_split(base64_encode($pdfdoc)); 
//Set who the message is to be sent from 
$mail->setFrom('[email protected]', 'Baby Diary'); 
//Set who the message is to be sent to 
$mail->addAddress('[email protected]', 'haha'); 
//Set the subject line 
$mail->Subject = 'PHPMailer mail() test'; 
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
$mail->AddStringAttachment($attachment, 'doc.pdf', 'base64', 'application/pdf'); 

if (!$mail->send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

입니다. 어떤 제안이라도 대단히 감사하겠습니다.

답변

1

문제는 phpmailer이며 fpdf는 아닙니다. 표준 PHPMailer 클래스 메소드를 사용해보십시오.

것은 내가이 PHPMailer doc samples에 포함 된 첨부 파일 예에서 코드를 재 시도 거라고했다합니다. 그리고 특히 나는 PHPMailer 메서드를 사용하는 대신에 내 자신의 smtp formating을 사용하는 방법을 고수 할 것입니다.

그래서 첫번째 파일에 PDF 파일을 저장 한 다음 $mail->addAttachment()으로 파일을 첨부 해보십시오. 그리고 PHPMailer가 메일 포매팅을 처리하게하십시오.

require('fpdf.php'); 
require 'PHPMailerAutoload.php'; 
header("Access-Control-Allow-Origin: *"); 
$pdf = new FPDF(); 
$pdf->AddPage(); 

$pdf->SetFont('Arial','UB',28); 
$pdf->Cell(0,10,"My Profile",0,1,'C'); 
$pdf->Ln(); 

$pdfoutputfile = '/some-tmp-dir/temp-file.pdf'; 
$pdfdoc = $pdf->Output($pdfoutputfile, 'F'); 

// We're done with the pdf generation 
// now onto the email generation 

$mail = new PHPMailer(); 
$mail->isSMTP();     // Set mailer to use SMTP 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;   // Enable SMTP authentication 
$mail->Username = '[email protected]'; // SMTP username 
$mail->Password = 'password';   // SMTP password 
$mail->SMTPSecure = 'tls';  // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587; 
$separator = md5(time());  // a random hash will be necessary to send mixed content 

//Set who the message is to be sent from 
$mail->setFrom('[email protected]', 'From Test address'); 

//Set who the message is to be sent to 
$mail->addAddress('[email protected]', 'to test address'); 

//Set the subject line 
$mail->Subject = 'Test message with attachement'; 
$mail->Body = 'Test message with attached files.'; 

$mail->AddAttachment($pdfoutputfile, 'my-doc.pdf'); 

if (!$mail->send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 
+1

와우가이 방식으로 작동합니다 :

그래서 아래 코드 (난 당신이 아마 미세 조정이 필요합니다, 그것을 실행하지 않은) 같은 뭔가 끝낼 수 있습니다! 게다가, 나는 같은 시간에 내 서버에 저장할 수 있습니다! –

관련 문제