i try to send email in laravel 5.2 and its works.
my problem is how to convert a view with its data to PDF then attach it to email
i try this
$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');
});
the error is time out
please i need your help in this
Edit: Sorry, your question is based on Laravel 5.2 and my answer is based on Laravel 5.4. As of generating a PDF can still be done with the DOMPDF package, and the docs for attaching it to a mail can be found in the official Laravel docs here
Generating a PDF based on a view template can be easily done by using a package such as DOMPDF made by Barryvdh
Generating a PDF would look something like this
$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);
Attaching a document to a mail is pretty simple in 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#stackoverflow.com', 'From')->subject('New mail')
->with([
'name' => $this->data['name'],
])->attach($file, [
'as' => 'File name',
'mime' => 'application/pdf',
]);
i try the following
$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',
]);
});
i saves the document in Storeag
but also the same
time out!!!!
so the message not sent.
thanks all of you.. i solve it. it was something like this
$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',
]);
});
Related
Here is my controller
I would like to attach a file that was requested from Postman API, please help me to solve my problem.
$p = new PersonAskPermission();
$p->start = $request->start;
$p->end = $request->end;
$p->day = $request->day;
$p->person_id = $request->person_id;
$fileTemp = $request->file('file');
$fileExtension = $fileTemp->getClientOriginalExtension();
$fileName = Str::random(4). '.'. $fileExtension;
$path = $fileTemp->storeAs(
'apiDocs', $fileName
);
$p->getTypes()->attach($p_type);
$p->getReasons()->attach($p_reason);
return response()->json([
'message' => 'Permission Reason created successfully!',
'data' => $p
]);
}
Recently I wanted to separate my project in different services to I wanted to make blogs independent from the project.
In the first project i have written this code. I want to send the data that i get from the form to another API http://127.0.0.1:100/api/saveBlog
public function update(Request $request, $blog)
{
if (!$blog instanceof Blog) {
$blog = $this->getById($blog);
}
$response = Http::post("http://127.0.0.1:100/api/saveBlog",[
'name' => $request->input('name'),
'description' => $request->input('description'),
'name' => $request->input('name'),
'photto' => $request->file('photto')
]);
dd($response->status());
}
In the API service i am trying to read the data
Route::post("/saveBlog",function (Request $request){
$blog = new Blog();
$blog->name = $request->input('name');
$blog->description = $request->input('description');
$blog->name = $request->input('name');
$main = $request->file('photto');
$fileName = microtime() . '.' . $main->getClientOriginalExtension();
$img = Image::make($main->getRealPath());
$img->resize(400, 400);
$img->stream();
Storage::disk('local')->put('public/blogs/' . $fileName, $img, 'public');
$blog->image_path = "/storage/blogs/" . $fileName;
return $blog->save();
});
But i am getting 500 status error and blog is not being saved in database.
I think the problem is with $request->file('photto')
ANY IDEA?
check whether image exist in request like below
if($request->has('photto')){
$main = $request->file('photto');
$fileName = microtime() . '.' . $main->getClientOriginalExtension();
$img = Image::make($main->getRealPath());
$img->resize(400, 400);
$img->stream();
Storage::disk('local')->put('public/blogs/' . $fileName, $img, 'public');
$blog->image_path = "/storage/blogs/" . $fileName;
}
Updates
$photo = fopen(public_path('/storage/filename'), 'r');
$response = Http::
attach('photo', $photo)
->post($url, [
'param_1' => 'param_1 contents',
...
]);
Please i want to change the default laravel configuration from storage/app/public to public/media on the server. In localhost it worked with storage:link but on the server it isn't working even after adding a symlink. Below is my symlink code.
<?php symlink('/home/cemo/cem/storage/app/public','/home/cemo/public_html');
Also if i return the public_path() from the store function i get /home/cemo/cem/public
this is the structure of my cpanel
below is my store function using image intervention
public function store(Request $request)
{
return public_path();
$this->validate($request,[
'title'=>'required|min:6',
'body'=>'required'
]);
if($request->hasFile('image')){
$img = $request->file('image');
$imgName = time().'-'.uniqid() . '.' . $img->getClientOriginalExtension();
}
else{
$imgName = 'default.jpg';
}
$posts = new post;
$posts->title = $request->title;
$posts->body = $request->body;
$posts->posted_by = $request->posted_by;
$posts->status = $request->status;
$posts->position = $request->position;
$posts->image = $imgName;
$posts->source = $request->source;
$posts->save();
$posts->tags()->sync($request->tags);
if(!empty($img)){
Image::make($img)->resize(1500,550)->save(public_path('/media/images/blog/'. $imgName));
}
$notification = array(
'message' => 'Successfully created',
'alert-type' => 'success'
);
return redirect(route('post.index'))->with($notification);
}
In config/filesystems.php
Change the array to:
'root' => 'public/media',
I am working on sending email function. Firstly, I want to generate .pdf file from my view. Then I want to attach the generated .pdf file by email without saving it into disk. I use below in my controller:
$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name))
->attachData($pdf->output(), "newfilename.pdf");
And I get this error: "Call to a member function attachData() on null"
If I use below without attachment, it works well:
$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name));
Please advise.
I think you need to attach it to the message, not to the mailer.
$pdf = PDF::loadView('getpdf', $data);
$message = new Mysendmail($post_title, $full_name);
$message->attachData($pdf->output(), "newfilename.pdf");
Mail::to($to_email)->send($message);
Just use 'mime' => 'application/pdf', at last of your code. Simple!
$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name))
->attachData($pdf->output(), "newfilename.pdf"), [
'mime' => 'application/pdf',
]);
Just use 'mime' => 'application/pdf', at last of your code. Simple!
`
$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name))
->attachData($pdf->output(), "newfilename.pdf"), [
'mime' => 'application/pdf',
]);
`
I'm generating a CSV, and I want Laravel to force its download, but the documentation only mentions I can download files that already exist on the server, and I want to do it without saving the data as a file.
I managed to make this (which works), but I wanted to know if there was another, neater way.
$headers = [
'Content-type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="download.csv"',
];
return \Response::make($content, 200, $headers);
I also tried with a SplTempFileObject(), but I got the following error : The file "php://temp" does not exist
$tmpFile = new \SplTempFileObject();
$tmpFile->fwrite($content);
return response()->download($tmpFile);
Make a response macro for a cleaner content-disposition / laravel approach
Add the following to your App\Providers\AppServiceProvider boot method
\Response::macro('attachment', function ($content) {
$headers = [
'Content-type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="download.csv"',
];
return \Response::make($content, 200, $headers);
});
then in your controller or routes you can return the following
return response()->attachment($content);
A Laravel 7 approach would be (from the docs):
$contents = 'Get the contents from somewhere';
$filename = 'test.txt';
return response()->streamDownload(function () use ($contents) {
echo $contents;
}, $filename);
Try this:
// Directory file csv, You can use "public_path()" if the file is in the public folder
$file= public_path(). "/download.csv";
$headers = ['Content-Type: text/csv'];
//L4
return Response::download($file, 'filename.csv', $headers);
//L5 or Higher
return response()->download($file, 'filename.csv', $headers);