在 PHPMailer 中使用 PDF 的 AddStringAttachment 不起作用 [英] AddStringAttachment with PDF in PHPMailer not work

查看:60
本文介绍了在 PHPMailer 中使用 PDF 的 AddStringAttachment 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾使用 phpmailer 发送电子邮件.现在我必须在电子邮件中附加 pdf 文件.它附加了pdf,但该pdf无法打开.并且显示格式有问题.AddStringAttachment 对 pdf 不起作用吗?我应该怎么办?

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!
}

谢谢

推荐答案

您可以使用另一个 PHP 类创建 .pdf TCPD.您可以在示例中看到它是如何进行的.如果您不想将 PDF 保存在服务器中,您可以像以前一样发送,您只需要更改 TCPPDF $pdf->... 代码的输出行,如下所示:

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)

然后你可以发送它:

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

这篇关于在 PHPMailer 中使用 PDF 的 AddStringAttachment 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆