将SwiftMailer与Laravel 4一起使用附加动态文件

时间:2014-04-03 13:20:11

标签: php laravel laravel-4 swiftmailer

我正在尝试将一个PDF文件附加到我在Laravel 4中的外发电子邮件中,但我似乎无法使其正常工作。此设置不会返回任何错误或任何内容,只是在空白屏幕停止。电子邮件服务器设置正确,Blade视图正常工作。 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 个答案:

答案 0 :(得分:0)

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

   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));
    });