在 Laravel 中将生成的 pdf 附加到邮件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42993519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attach generate pdf into mail in Laravel
提问by Christianus Andre
I've some problem with email attach generate pdf, hope you can give me some advice, please kindly help.
我在电子邮件附件生成 pdf 时遇到了一些问题,希望您能给我一些建议,请帮助。
Here is the Controller:
这是控制器:
public function kirim(Request $request){
$keluhan = keluhan::findOrFail($request->id);
$tindak = DB::table('tindakans')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('tindakans.id, id_keluhan, perbaikan_sementara, revisi_dokumen, target_verifikasi, ttd_tanggung1,
ttd_tanggung2'))->get();
$analisa = DB::table('analisas')
->join('tindakans','tindakans.id','=','analisas.id_tindakan')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('id_tindakan, analisa, tindakan, pic, tanggal_pelaksanaan'))->get();
$pdf = \PDF::loadView('laporan.ptkp',compact('keluhan','tindak','analisa','halaman'));
//return $pdf->stream();
$data = array(
'email_address'=>$request->email_address,
'cc'=>$request->cc,
'subject'=>$request->subject,
'keterangantambahan'=>$request->keterangantambahan
);
Mail::send('laporan.kirim', $data, function($message) use($data) {
$message->from('[email protected]', 'PuraBox');
$message->to($data['email_address']);
if($data['cc'] != null){
$message->cc($data['cc']);
}
$message->subject($data['subject']);
$message->Attach($pdf);
});
return redirect('/');
}
How can I attach $pdf
?
我怎样才能附加$pdf
?
回答by Jo?o Mantovani
You are using:
您正在使用:
$message->Attach($pdf);
To add attachments to an e-mail, use the attach method on the $message object passed to your Closure. The attach method accepts the full path to the file as its first argument:
要向电子邮件添加附件,请在传递给闭包的 $message 对象上使用 attach 方法。attach 方法接受文件的完整路径作为它的第一个参数:
You should use the attach
method (without the capitalize letter), and in the parameter, you need to pass the path where the pdf is, not the generated pdf. like the docs says.
您应该使用该attach
方法(不带大写字母),并且在参数中,您需要传递pdf所在的路径,而不是生成的pdf。就像文档说的那样。
$message->attach($pathToFile);
In the end, will be something like that:
最后,将是这样的:
Mail::send('laporan.kirim', $data, function($message) use($data) {
$message->from('[email protected]', 'PuraBox');
$message->to($data['email_address']);
if($data['cc'] != null){
$message->cc($data['cc']);
}
$message->subject($data['subject']);
//Full path with the pdf name
$message->attach('foo/bar/mypdfname.pdf');
});
回答by NitinKaware
I don't know which library you are using for PDF generation, but I guess there should be an API something like this,
我不知道您使用哪个库来生成 PDF,但我想应该有这样的 API,
$pdf = \PDF::loadView('laporan.ptkp',compact('keluhan','tindak','analisa','halaman'));
$path = storage_path('app/public/pdf/')."example.pdf";
$pdf->save($path);
return $path;
And then you can use the path to attach to email.
然后您可以使用该路径附加到电子邮件。
Hope this helps.
希望这可以帮助。
回答by Adam Brzeziński
For future Googlers:
对于未来的 Google 员工:
Since Laravel 5.1you can use attachData()
method to directly attach generated file without the need for saving it.
从 Laravel 5.1 开始,您可以使用attachData()
method 直接附加生成的文件,而无需保存它。
Source:
Manual - section Raw Data Attachments
The attachData method accepts the raw data bytes as its first argument, the name of the file as its second argument, and an array of options as its third argument:
attachData 方法接受原始数据字节作为它的第一个参数,文件名作为它的第二个参数,以及一个选项数组作为它的第三个参数:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.orders.shipped')
->attachData($this->pdf, 'name.pdf', [
'mime' => 'application/pdf',
]);
}
回答by Niyaz Ahamed
May be you can do something as
也许你可以做一些事情
Mail::send('laporan.kirim', $data, function($message) use($data) {
$message->from('[email protected]', 'PuraBox');
$message->to($data['email_address']);
if($data['cc'] != null){
$message->cc($data['cc']);
}
$message->subject($data['subject']);
$message->Attach($pdf->output(),"name.pdf");
});
return redirect('/');
}
}
this may help you out
这可能会帮助你