在wp_mail()中自动生成附件文件

时间:2017-02-05 17:51:08

标签: wordpress email

我正在尝试发送带有附件的邮件,该附件根据订阅选项自动生成。但每次发送邮件(在垃圾邮件中),但附件不是。我在这里搜索了这个主题,但所有解决方案都与上传内容有关。夹。

这是我的代码..

require ( ABSPATH . 'pdfcrowd.php');
                    try
                    {
                        // create an API client instance
                        $client = new Pdfcrowd("apiname", "apikay");

                        // convert a web page and store the generated PDF into a $pdf variable
                        $pdf = $client->convertFile( ABSPATH . 'invoice_html.php');

                        // set HTTP response headers
                        header("Content-Type: application/pdf");
                        header("Cache-Control: max-age=0");
                       header("Accept-Ranges: none");
                       header("Content-Disposition: attachment; filename=\"invoice.pdf\"");

                        //$to = $invoice_email;
                        $to = "moyen@gmail.com";
                        $subject = "Invoice for your online package.";
                        $message = "Message Body Invoice for your online package. Invoice for your online package. Invoice for your online package";
                        $headers = array('Content-Type: text/html; charset=UTF-8','From: My Site Name <uddin@gmail.com');
                        $attachments = $pdf;

                        // send the generated PDF
                        //echo $attachments;

                       $wp_mail = wp_mail( $to, $subject, $message, $headers, $attachments );

                    }
                    catch(PdfcrowdException $why)
                    {
                        echo "Pdfcrowd Error: " . $why;
                    }

任何帮助?

N.B:因为我设法在浏览器中输出pdf文件以保存,但我想将此pdf文件保存在目录中,然后将作为附件发送。上面这段代码的最佳效果是什么?

由于

1 个答案:

答案 0 :(得分:0)

最后,我可以通过以下修改代码在电子邮件中发送pdf ....

1。首先,我将转换后的pdf保存在uploads文件夹中,然后

2。我把pdf作为附件

require ( ABSPATH . 'pdfcrowd.php');
                    try
                    {
                    // create an API client instance
                    $client = new Pdfcrowd("apiname", "apikay");

                    // converted php file and store the generated PDF inside uploads

                    $fd = fopen( ABSPATH . 'wp-content/uploads/invoice.pdf', 'wb');
                    $client->convertFile( ABSPATH . 'invoice_html.php', $fd );
                    fclose($fd);

                    // set HTTP response headers
                    header("Content-Type: application/pdf");
                    header("Cache-Control: max-age=0");
                   header("Accept-Ranges: none");
                   //header("Content-Disposition: attachment; filename=\"invoice.pdf\"");

                    //$to = $invoice_email;
                    $to = "moyen@gmail.com";
                    $subject = "Invoice for your online package.";
                    $message = "Message Body Invoice for your online package. Invoice for your online package. Invoice for your online package";
                    $headers = array('Content-Type: text/html; charset=UTF-8','From: My Site Name <uddin@gmail.com');

     // take the file from the uploads folder
                    $attachments = array( ABSPATH . '/wp-content/uploads/invoice.pdf' );


                    // send the generated PDF
                    //echo $attachments;

                   $wp_mail = wp_mail( $to, $subject, $message, $headers, $attachments );

                }
                catch(PdfcrowdException $why)
                {
                    echo "Pdfcrowd Error: " . $why;
                }