如何在laravel电子邮件中附件

时间:2017-04-18 14:29:36

标签: laravel email attachment email-attachments

我尝试在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');

});

错误是超时 拜托,我需要你的帮助

3 个答案:

答案 0 :(得分:2)

编辑:对不起,您的问题基于Laravel 5.2,我的回答基于Laravel 5.4。至于生成PDF仍然可以使用here包完成,并且可以在Laravel官方文档中找到将其附加到邮件的文档DOMPDF

使用Barryvdh制作的[docs]等软件包可以轻松完成基于视图模板生成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)Why is RestSharp deserializing two dates differently?

中,

将文档附加到邮件非常简单

// file location
$file = storage_path('app/folder/your-file.pdf');

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

答案 1 :(得分:1)

我尝试以下

$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中 但也一样 超时!!!! 所以邮件没有发送。

答案 2 :(得分:0)

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

$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',
                ]);
                });