Reputation: 237
I've some problem with email attach generate pdf, hope you can give me some advice, please kindly help.
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
?
Upvotes: 4
Views: 14784
Reputation: 2480
it's very simple just add your blade in the loadView() method and pass it's pdf instance into callback
public function index()
{
$data["email"] = "[email protected]";
$data["title"] = "cdlcell";
$data["body"] = "Demo";
$pdf = PDF::loadView('emails.myTestMail', $data);
Mail::send('emails.myTestMail', $data, function($message)use($data, $pdf) {
$message->to($data["email"], $data["email"])
->subject($data["title"])
->attachData($pdf->output(), "text.pdf");
});
dd('Mail sent successfully');
}
Upvotes: 1
Reputation: 161
Since Laravel 5.1 you can use attachData()
method to directly attach generated file without the need for saving it.
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:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.orders.shipped')
->attachData($this->pdf, 'name.pdf', [
'mime' => 'application/pdf',
]);
}
Upvotes: 10
Reputation: 51
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
Upvotes: 0
Reputation: 41
I don't know which library you are using for PDF generation, but I guess there should be an API something like this,
$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.
Upvotes: 2
Reputation: 2398
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:
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.
$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');
});
Upvotes: 3