PHP发送带有PDF附件的电子邮件 - php

我正在使用FPDF创建pdf。 Pdf生成完美,电子邮件中也可以使用pdf。但我也想发送正文消息。我已经尝试过用身体信息。示例细文本消息This is text message from shohag,但只有pdf附件可用,正文为空。这是我的代码。

function send_pdf_to_user(){
    if($_REQUEST['action'] == 'pdf_invoice' ){
        require('html2pdf.php');
        $pdf=new PDF_HTML();
        $pdf->SetFont('Arial','',11);
        $pdf->AddPage();

        $text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']);
        if(ini_get('magic_quotes_gpc')=='1')
        $text=stripslashes($text);
        $pdf->WriteHTML($text);

        //documentation for Output method here: http://www.fpdf.org/en/doc/output.htm
        $attach_pdf_multipart = chunk_split( base64_encode( $pdf->Output( '', 'S' ) ) );


        //define the receiver of the email 
        $to = '[email protected]';

        //define the subject of the email 
        $subject = 'Test Invoice'; 
        //create a boundary string. It must be unique 
        //so we use the MD5 algorithm to generate a random hash 
        $random_hash = md5(date('r', time())); 
        //define the headers we want passed. Note that they are separated with \r\n 
        $headers = "From: [email protected]\r\nReply-To: [email protected]"; 
        //add boundary string and mime type specification 
        $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";       



        $msg .= "Content-Type: application/octet-stream; name=\"attachment.pdf\"\r\n";
        $msg .= "Content-Transfer-Encoding: base64\r\n";
        $msg .= "Content-Disposition: attachment\r\n";
        $msg .= $attach_pdf_multipart . "\r\n";

        $msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
        $msg .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $msg .= "<p>This is text message from shohag</p>\r\n\r\n";  

        global $message;
        $message = '';
        $mail_sent = @mail( $to, $subject, $msg, $headers );
        //@mail( $to1, $subject, $msg, $headers );
        if(!empty($mail_sent)):
            $message = "Invoice sent succuessfully";
        else:
            $message = "Error occured. Please try again.";
        endif;
    }
}

请检查我的代码,并让我知道其他可能性。提前致谢。

参考方案

您可以将PHPMailer与FPDF一起使用。它工作正常,没有任何麻烦。您需要更改$pdf->Output的参数。下载class.phpmailer.phpPHPMailerAutoload.php并将其复制到您的工作文件夹中。在class.phpmailer.php下方或上方附加require('html2pdf.php');。我之前已经做过,所以这可以工作。根据您的代码,这应该工作。

function send_pdf_to_user(){
    if($_REQUEST['action'] == 'pdf_invoice' ){
        require('html2pdf.php');
        require_once('class.phpmailer.php');
        $pdf=new PDF_HTML();
        $pdf->SetFont('Arial','',11);
        $pdf->AddPage();

        $text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']);
        if(ini_get('magic_quotes_gpc')=='1')
        $text=stripslashes($text);
        $pdf->WriteHTML($text);

        $mail = new PHPMailer(); // defaults to using php "mail()"
        $body = "This is test mail by monirul";

        $mail->AddReplyTo("[email protected]","Test Lernt");
        $mail->SetFrom('[email protected]', 'Test Lernt');

        $address = "[email protected]";
        $mail->AddAddress($address, "Abdul Kuddos");       
        $mail->Subject    = "Test Invoice";       
        $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

        $mail->MsgHTML($body);
        //documentation for Output method here: http://www.fpdf.org/en/doc/output.htm       
        $pdf->Output("Test Invoice.pdf","F");
        $path = "Walter Lernt Invoice.pdf";

        $mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
        global $message;
        if(!$mail->Send()) {
          $message =  "Invoice could not be send. Mailer Error: " . $mail->ErrorInfo;
        } else {
          $message = "Invoice sent!";
        }

    }
}

PHP-正则表达式删除引号并添加大括号? - php

好吧,我不愿承认这一点,但是我对REGEX感到很困难,我永远找不到关于如何设置表达式的不错的教程。所以说我有这样的事情context['something'] 我想将所有事件更改为context[something] 那我有' . $var . ' 我想将所有事件更改为{var} 这是当前的概念,但是我在正则表达式部分…

使用jQuery将值增加到attr - php

我想增加i的值。 “ for”循环不起作用。$("a[href$='.xls']").appendTo(".xl1").attr('id','xl'+i); 我搜索所有excel文件,并将它们放在容器中并增加其id的值。谢谢吉恩 参考方案 $("a[…

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…