AddStringAttachment with PDF in PHPMailer not work

99封情书 提交于 2019-12-11 19:20:04

问题


I had used phpmailer for send email. Now i have to attach pdf file in email. It attached pdf but that pdf could not open. and is shows there is problem in format. Is AddStringAttachment doesn't work for pdf? What should i do?

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   = 'nabina.smartmobe@gmail.com'; // SMTP account username
        $mail->Password   = '*******';        // SMTP account password
        $mail->AddAddress('nabina.shahi@gmail.com','nabina');

        $mail->SetFrom('no-reply@nayacinema.com.np', 'no-reply@nayacinema.com.np');
        $mail->AddReplyTo('no-reply@nayacinema.com.np', '');
        $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!
}

Thank you


回答1:


You can create a .pdf with another PHP class TCPDF. You can see how it goes in examples. And if you dont want to save the PDF in the server you can send like you did, you only need to change the output line of the TCPDF $pdf->... codes, like this:

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

then you can send it:

$mail->AddStringAttachment($attachdata, 'Filename.pdf');


来源:https://stackoverflow.com/questions/22007292/addstringattachment-with-pdf-in-phpmailer-not-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!