2014-02-25 70 views
0

我曾經使用phpmailer發送電子郵件。現在我必須在電子郵件中附上pdf文件。它附有pdf,但該pdf無法打開。並顯示格式有問題。 AddStringAttachment不適用於pdf嗎?我該怎麼辦?在PHPMailer中使用PDF的AddStringAttachment不起作用

require_once('././php_mailer/class.phpmailer.php'); 
$this->load->helper('mail_html'); 
$body2 = "You are receiving this email because the Transfer Application submitted for transferring to g is missing required documentation. 
Please see the note below for details. The transfer application will not be reviewed until all of the necessary materials are received by the UHSAA. 

"; 
$body = 'test'; 


$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch 

$mail->IsSMTP(); // telling the class to use SMTP 
    try { 
     $mail->Host  = 'smtp.gmail.com'; // SMTP server 
     $mail->SMTPDebug = 0;      // enables SMTP debug information (for testing) 
     $mail->SMTPAuth = true;     // enable SMTP authentication 
     $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail 
     $mail->Host  = 'smtp.gmail.com'; // sets the SMTP server 
     $mail->Port  = 465;     // set the SMTP port for the GMAIL server 
     $mail->Username = '[email protected]'; // SMTP account username 
     $mail->Password = '*******';  // SMTP account password 
     $mail->AddAddress('[email protected]','nabina'); 

     $mail->SetFrom('[email protected]', '[email protected]'); 
     $mail->AddReplyTo('[email protected]', ''); 
     $mail->IsHTML(true); 
     $mail->Subject ='NayaCinema: Test'; 
     $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically 
     $mail->MsgHTML($body); 
     $mail->AddStringAttachment($body2, 'Filename.pdf', 'base64', 'application/pdf'); 


    if($mail->Send()){ 
     echo 'sent'; die; 
     return true; 
    }else{ 
     echo ' not sent'; die; 
     return false; 
    } 
} catch (phpmailerException $e) { 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
} catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
} 

謝謝

回答

1

您可以創建其他PHP類TCPDF一個PDF。你可以看到它的例子。如果你不想在PDF保存在服務器上,您可以發送像你這樣,你只需要改變TCPDF $ PDF格式的輸出線 - > ...的代碼,像這樣:

$attachdata = $pdf->Output('foo.pdf','S'); // return the document as a string (name is ignored) 

然後你可以發送它:

$mail->AddStringAttachment($attachdata, 'Filename.pdf'); 
相關問題