I want to create a PDF using the barryvdh/laravel-dompdf package and send this with an email as attachment.
The code I have now is:
$pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));
Mail::queue('emails.factuur', array('factuur' => $factuur), function($message) use ($pdf)
{
$message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
$message->attach($pdf->output());
});
But now I get the following error:
Serialization of 'Closure' is not allowed
You can only send serializable entities to the queue. This includes Eloquent models etc. But not the PDF view instance. So you will probably need to do the following:
Mail::queue('emails.factuur', array('factuur' => $factuur), function($message)
{
$pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));
$message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
$message->attach($pdf->output());
});
Related
I'm trying to add export functionalities to my Laravel app. I'd like to export db data into an excel spreadsheet. I'm using the Maatwebsite package.
I'm using Laravel 7.12 and 3.1.19 of the Maatwebsite package.
I'm getting the following error when trying to export the data:
Call to undefined method Maatwebsite\Excel\Excel::create()
I added the foloowing use statement to my controller:
use Maatwebsite\Excel\Facades\Excel;
And registered the followings in config/app.php
Maatwebsite\Excel\ExcelServiceProvider::class,
in providers and
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
in the aliases section
Here is my function from the controller file:
public function excel() {
$subscribers = DB::table('subscribers')->get()->toArray();
// Use this for excel spreadsheet header
$subscriber_array[] = array('Name', 'Email');
// Convert subscriber data from php object to array and store them under $subscriber_array
foreach($subscribers as $subscriber) {
$subscriber_array[] = array(
'Name' => $subscriber->name,
'Email' => $subscriber->email
);
}
// "Subscriber Data" will be the name of the generated excel file
Excel::create('Subscriber Data', function($excel) use ($subscriber_array) {
$excel->setTitle('Subscriber Data');
$excel->sheet('Subscriber Data', function($sheet) use ($subscriber_array) {
$sheet->fromArray($subscriber_array, null, 'A1', false, false);
});
})->download('xlsx');
}
Did you try this?
use Maatwebsite\Excel\Facades\Excel;
OR
use Excel
i have a query to return all the the data in visits table
$visits = Visit::get();
i want to pass the returned data in $data so that i can send it on mail and display the data in the email body
$data = array();
Mail::send('mails.mail', $data, function ($message) use ($host_email, $to_name) {
$message->from('', '');
$message->to($host_email);
$message->subject('Visitor arrived');
});
how do i do that
You can use Laravel structure, You can create a mail class with this command:
php artisan make:mail OrderShipped
it will generate a class in App\Mail path sample named OrderShipped in build method you can call a blade view for this mail and send data to it like:
public function build()
{
return $this->view('emails.orders.shipped')
->with([
'orderName' => $this->order->name,
'orderPrice' => $this->order->price,
]);
}
and finally use this to send email to user:
Mail::to($request->user())->send(new OrderShipped($order));
look here for full documentation:
laravel mail
I want to attach pdf generated with tcpdf library without save.
I'm able to attach the pdf generated but it's corrupt.
I search a lot examples but any seems don't work
This my code:
public function index($id) {
$viaje = Viaje::find($id);
$users = User::orderBy('id', 'asc')->get();
// usersPdf is the view that includes the downloading content
$view = \View::make('usersPdf', ['viaje' => $viaje, 'users' => $users]);
$html_content = $view->render();
// Set title in the PDF
PDF::SetTitle("List of users");
PDF::AddPage();
PDF::writeHTML($html_content, true, false, true, false, '');
//PDF::Output('userlist.pdf');
$fileatt = PDF::Output($name='yourfilename.pdf', $dest='E');
$pdf = chunk_split($fileatt);
$contactopro = Contactoviajespro::find($id);
$data = [
'link' => 'http://',
'contacto' => $contactopro->name,
];
Mail::send('emails.notificacion', $data, function($msg) use($pdf) {
$msg->from('administracion#buendialogistica.com', 'Javier');
$msg->to('xavieeee#gmail.com')->subject('Notificación');
$msg->attachData($pdf, 'orden.pdf');
});
return redirect()->route('home')
->with(['message' => 'Email enviado correctamene']);
}
Use "S" to generate pdf and do not do chunk_split(), Laravel will do that. Additionally, if you are using queue() instead of send(), it will fail because of the attachment. To queue, write a job and send with the job queue.
The following IS working when I use Mail::send
$email = 'my#email.com';
$name = 'My Name';
$invoice = InvoicePdf::generate($invoice_id); // generates PDF as raw data
Mail::send('mail.template', null, function($message) use ($name, $email, $invoice) {
$message->to($email, $name);
$message->subject('Thank you for your order!');
$message->attachData($invoicePdf, 'invoice.pdf', ['mime' => 'application/pdf']);
});
It works fine and an email is generated with the correct PDF attachment.
However, if I change Mail::send to Mail::queue then I receive the following error:
Unable to JSON encode payload. Error code: 5
/var/www/html/october/vendor/laravel/framework/src/Illuminate/Queue/Queue.php
line 90
If I take the $message->attachData(); line out then it works even with Mail::queue so it seems like the raw data from the attachment is causing issues with the queue but there's nothing in the relevant October or Laravel docs about how to deal with this.
May be its because $invoicePdf data is raw data of PDF file and php can not process that data (attachData) when saving to database.
hmm, alternative you can generate file and then just attach file path to mail and then add to queue.
// generate tempfile name
$temp_file = tempnam(sys_get_temp_dir(), 'inv');
// this pdf is generated by renatio plugin but you can
// use your data and save it to disk
PDF::loadTemplate('renatio::invoice')
->save($temp_file);
Mail::queue('mail.template', null, function($message) use ($name, $email, $temp_file) {
$message->to($email, $name);
$message->subject('Thank you for your order!');
$message->attach($temp_file, ['as' => 'Your_Invoice.pdf', 'mime' => 'application/pdf']);
});
it should work.
#Joseph pointed that in Laravel 5.5 there is mailable which can be used. #Joseph pointed this solution and it seems working so you can also use this solution if your laravel version is >= 5.5
https://laracasts.com/discuss/channels/laravel/sending-email-with-a-pdf-attachment
Thanks #Joseph
when i am trying to send Mail through Contact Us Form receiving this Error
"Address in mailbox given [] does not comply with RFC 2822, 3.6.2."
I try search to find solution but I cannot find one. I edited config/mail.php
public function sendContactInfo(ContactMeRequest $request)
{
$data = $request->only('name', 'email');
$emailto="******#gmail.com";
$data['messageLines'] = explode("\n", $request->get('message'));
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});
return back()
->withSuccess("Thank you for your message. It has been sent.");
}
with configuration file
i am following this tutorial
Laravel Send Mail
use $data['email']
Mail::send('publicPages.contactus', $data, function ($message) use ($emailto,$data['email']) {
$message->subject('Contact Us Form: ')
->to(config('blog.contact_email'))
->replyTo($data['email']);
});