dsfdsf23423 2014-02-25 07:22
浏览 91
已采纳

在PHPMailer中使用PDF的AddStringAttachment不起作用

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条回答 默认 最新

  • doutou19761022 2014-05-20 17:22
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法