如何在 laravel 电子邮件中制作附件

How to make attachment in laravel email

我尝试在 laravel 5.2 及其作品中发送电子邮件。 我的问题是如何将视图及其数据转换为 PDF,然后将其附加到电子邮件 我试试这个

$datastd['fname']=$printqu->std_fname;
$datastd['mname']=$printqu->std_mname;
$datastd['lname']=$printqu->std_lname;
$datastd['email']=$printqu->std_email;
$datastd['email']=$printqu->std_email;
$datastd['orgname']=$printqu->name;
$datastd['depname']=$printqu->Dep_Name;




Mail::send('email.train_form',['datastd'=>$datastd], function($mail) use ($datastd){

    $mail->to($datastd['email'],$datastd['fname'],$datastd['mname'],$datastd['lname'],$datastd['orgname'],$datastd['depname'])->attachData($datastd, 'printPreviewm.pdf')->from('everyone@gmail.com')->subject('Training Forms');

});

错误超时 在这方面我需要你的帮助

编辑:抱歉,您的问题基于 Laravel 5.2,我的回答基于 Laravel 5.4。截至生成 PDF 仍然可以使用 DOMPDF package, and the docs for attaching it to a mail can be found in the official Laravel docs here


使用 Barryvdh

制作的 DOMPDF 等包可以轻松地基于视图模板生成 PDF

生成 PDF 看起来像这样

$view = View::make('any.view', compact('variable'));
$contents = $view->render();
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML($contents);
$output = $pdf->output();

Storage::put('/folder/your-file.pdf', $output);

将文档附加到邮件 在 Laravel (5.4) [docs]

中非常简单
// file location
$file = storage_path('app/folder/your-file.pdf');

// return mail with an attachment
return $this->view('emails.confirm')
    ->from('me@whosebug.com', 'From')->subject('New mail')
    ->with([
        'name' => $this->data['name'],
    ])->attach($file, [
        'as' => 'File name',
        'mime' => 'application/pdf',
    ]);

我尝试以下方法

$view = View::make('printPreview', compact('printqu','printqu2'));
$contents = $view->render();
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML($contents);
$output = $pdf->output();

Storage::put('app/folderletter/your-file.pdf', $output);

 $datastd['fname']=$printqu->std_fname;
 $datastd['mname']=$printqu->std_mname;
 $datastd['lname']=$printqu->std_lname;
 $datastd['email']=$printqu->std_email;
 $datastd['email']=$printqu->std_email;
 $datastd['orgname']=$printqu->name;
 $datastd['depname']=$printqu->Dep_Name;
$file = storage_path('app/folderletter/your-file.pdf');

   Mail::send('email.train_form',['datastd'=>$datastd], function($mail) use ($datastd,$file){
     //$pdf = PDF::loadView('printPreviewm',['datastd'=>$datastd]);
     $mail->to($datastd['email'],$datastd['fname'],$datastd['mname'],$datastd['lname'],$datastd['orgname'],$datastd['depname'])
     ->from('everyone@gmail.com')->subject('Training Forms')
     ->attach('app/folderletter/your-file.pdf', [
                    'as' => 'name.pdf',
                    'mime' => 'application/pdf',
                ]);
                });

我将文档保存在 Storeag 但也一样 暂停!!!! 所以消息没有发送。

谢谢大家..我解决了。是这样的

$view = View::make('printPreview', compact('printqu','printqu2'));
$contents = $view->render();
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML($contents);
$output = $pdf->output();

Storage::put('app/folderletter/your-file.pdf', $output);

 $datastd['fname']=$printqu->std_fname;
 $datastd['mname']=$printqu->std_mname;
 $datastd['lname']=$printqu->std_lname;
 $datastd['email']=$printqu->std_email;
 $datastd['email']=$printqu->std_email;
 $datastd['orgname']=$printqu->name;
 $datastd['depname']=$printqu->Dep_Name;

//$file= public_path(). "/app/".$sestest3->dep_path;
$file = storage_path(). "/app/app/folderletter/your-file.pdf";
   Mail::send('email.train_form',['datastd'=>$datastd], function($mail) use ($datastd,$file){
     //$pdf = PDF::loadView('printPreviewm',['datastd'=>$datastd]);
     $mail->to($datastd['email'],$datastd['fname'],$datastd['mname'],$datastd['lname'],$datastd['orgname'],$datastd['depname'])
     ->from('everyone@gmail.com')->subject('Training Forms')
     ->attach($file, [
                    'as' => 'name.pdf',
                    'mime' => 'application/pdf',
                ]);
                });