如何下载文件并立即导出为电子邮件附件

时间:2016-06-17 09:17:03

标签: php pdf mailto

我有一个网络应用,用户可以点击按钮下载PDF报告。

我的用户要求在下载PDF时立即将其作为电子邮件附件打开(有点像点击mailto锚点时)。

这甚至可能吗?我想也许可以使用js在幕后生成一个锚标签,但我读到mailto并不真正支持附件。

如果这很重要,则使用设置为下载输出模式的PHP mPDF生成PDF服务器端。

2 个答案:

答案 0 :(得分:0)

如果文件在本地可用,则

public static function getPrice($productCode, $purchaseType, $quantity){ return PriceList::where('product_code', $productCode) ->where('purchase_type', $purchaseType) ->where('minimum', '>=', $quantity) ->where('maximum', '<=', $quantity) ->get()->first()->unit_price; } 支持附件。您可以使用mailto参数指定该参数。
例如attach
但是,我不确定如何在代码中使用它,因为用户可以将文件下载到其计算机上的任何位置。如果它有用,mailto:lastname.firstname@xxx.com?subject=Test&attach=C:\Documents%20and%20Settings\username\Desktop\foldername\file.extn参数支持共享驱动器位置,如果用户具有适当的访问权限。

答案 1 :(得分:0)

为此,您必须分两步分开处理。

  1. 使用http://wkhtmltopdf.org/
  2. 在某个目录中的服务器上生成PDF
  3. 使用https://github.com/PHPMailer/PHPMailer
  4. 将生成的pdf作为电子邮件附件的一部分发送

    配置这两项服务将帮助您完成任务。

    以下是可以帮助您的示例代码

    //generate PDF file for bill and send it in email
    public function sendInvoiceInEmail($register_no, $html_string, $guest_email_id)
    {
        //create mailer class
        $mail = new PHPMailer;
        //remove old invoice file if exist at public/doc
        // here i have used my path you have to use your path 
        if(file_exists(__DIR__.'/../../public/docs/invoice.pdf')){
            unlink(__DIR__.'/../../public/docs/invoice.pdf'); 
        }
    
        // You can pass a filename, a HTML string, an URL or an options array to the constructor
        $pdf = new Pdf([
            'commandOptions' => [
                'useExec' => false,
                'escapeArgs' => false,
                'procOptions' => array(
                // This will bypass the cmd.exe which seems to be recommended on Windows
                'bypass_shell' => true,
                // Also worth a try if you get unexplainable errors
                'suppress_errors' => true,
                ),
            ],
        ]);
        $globalOptions = array(
            'no-outline'
        );
    
        $pdf->setOptions($globalOptions);
    
        $pdf->addPage($html_string);
        //for Windows in my system i have setup wkhtmltopdf here
        $pdf->binary = '"C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe"';
        // for linux when you will host you have to setp on linux abd comment windows path and uncomment below line
        //$pdf->binary = '/home/username/wkhtmltox/bin/wkhtmltopdf';
        if (!$pdf->saveAs(__DIR__.'/../../public/docs/invoice.pdf')) {
            throw new Exception('Could not create PDF: '.$pdf->getError());
        }
        //now send email and attach invoice.pdf file from public/doc/invoice.pdf
    
    
    
        //$mail->SMTPDebug = 3;                               // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'hostname';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'username';                 // SMTP username
        $mail->Password = 'password';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to
        $mail->setFrom('from_email_address', 'From Name',FALSE);
        $mail->addAddress('to_email_address');     // Add a recipient
    
        $mail->addAttachment(__DIR__.'/../../public/docs/invoice.pdf');         // Add attachments
    
        $mail->isHTML(true);                                  // Set email format to HTML
    
        $mail->Subject = 'Subject';
        $mail->Body    = 'email message body';
    
        $is_email_sent = false;
        if(!$mail->send()) {
            // echo 'Message could not be sent.';
            // echo 'Mailer Error: ' . $mail->ErrorInfo;
            $is_email_sent = false;
        } else {
            // echo 'Message has been sent';
            $is_email_sent = true;
        }
    
        return true;
    }