代码之家  ›  专栏  ›  技术社区  ›  Kevin

使用SwiftMailer和Laravel 4附加动态文件

  •  1
  • Kevin  · 技术社区  · 11 年前

    我正在尝试在Laravel 4中的发送邮件中附上一个PDF文件,但我似乎无法正常工作。此设置不会返回任何错误或任何内容,只是在空白屏幕上停止。电子邮件服务器设置正确,刀片式视图工作正常。PDF创建者也在工作。

    我在这里做错了什么?我对Laravel和PHP很陌生。感谢您提供的任何帮助或指导!

    public function getPdf()
    {
        // YOU NEED THIS FILE BEFORE YOU CAN RUN DOMPDF
        define('INCLUDE_PATH', '/home/dsimedic/apm/vendor/dompdf/dompdf');
        @ini_set("include_path", INCLUDE_PATH);
        require_once('dompdf_config.inc.php');
    
        // grab session data passed from the redirect
        $apmformdata = Session::get('apmform');
    
        // render the page with the correct data passed in
        $html = View::make('formPdf')
            ->with('apmformdata', $apmformdata);
    
        // setup and generate the PDF
        $dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        $pdf = $dompdf->output();
    
        // download the file to the server.
        $file_to_save = './pdf/'.$apmformdata->JobNumber.'.pdf';
        file_put_contents($file_to_save, $pdf);
    
        // route the followup response based on the id status
        if (($apmformdata->id) != "")
        {   
            // set the message displayed
            $status = 'updated';
    
            // send out the confirmation email with attached PDF
            Mail::later(20,'emails.formFollowup', array('status'=>$status, 'JobNumber'=>$apmformdata->JobNumber), function($message) {
                $message->to('name@domain.com', 'My Name')
                    ->subject('APM Form Alert Notice')
                    ->attach(use ($file_to_save));
            });
        }
        else
        {
            // set the message displayed
            $status = 'created';
        }
    
        // send user back to the homepage with success message
        return Redirect::to('/')
            ->with('flash_notice', 'APM Form: '.$apmformdata->JobNumber.' has been '.$status.'! <br/>You will recieve an email with a PDF copy of the form shortly!');
    }
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   Antonio Carlos Ribeiro    11 年前

    看起来您没有将文件传递给闭包:

       Mail::later(20,'emails.formFollowup', array('status'=>$status, 'JobNumber'=>$apmformdata->JobNumber), function($message) use ($file_to_save) {
            $message->to('name@domain.com', 'My Name')
                ->subject('APM Form Alert Notice')
                ->attach(use ($file_to_save));
        });