I have never sent images that were uploaded in a form to emails before, so I'm having a bit of trouble.
I know this line of code: $request->file('image')->move('/', $fileName);
won't do anything because of where I'm moving it to, but that's as far as I could get reading these Laravel docs. As a matter of fact, it spits out this error:
Unable to write in the "/" directory
If I were to remove the above code and submit my form, everything works properly but the image fields shows up empty. That leads me to my question:
Where do I move the image to, and how do I get it from that location so that it can be used in the sent email rather than showing a blank spot as if nothing were uploaded?
Here are my files...
index.blade.php (image part of form only):
<div class="form-group">
{!! Form::file('image') !!}
</div>
emails/contact.blade.php (image part only):
<p>
Image: {{ $image }}
</p>
Requests/ContactFormRequest.php:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class ContactFormRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required',
'height' => 'required',
'age' => 'required',
'weight' => 'required',
'gender' => 'required',
'email' => 'required|email',
'image' => 'required|mimes:jpeg,jpg,png|max:200px'
];
}
}
Auth/AboutController.php:
<?php namespace App\Http\Controllers;
use App\Http\Requests\ContactFormRequest;
class AboutController extends Controller {
public function create()
{
return view('pages.index');
}
public function store(ContactFormRequest $request)
{
$fileName = "Image";
$request->file('image')->move('/', $fileName);
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'height' => $request->get('height'),
'age' => $request->get('age'),
'weight' => $request->get('weight'),
'gender' => $request->get('gender'),
'email' => $request->get('email'),
'image' => $request->get('image')
), function($message)
{
$message->from('[email protected]');
$message->to('[email protected]', 'Admin')->subject('Example Text');
});
return \Redirect::route('/')
->with('message', 'Your application is on its way!');
}
}
if you want to attach a picture to a mail use this:
Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'height' => $request->get('height'),
'age' => $request->get('age'),
'weight' => $request->get('weight'),
'gender' => $request->get('gender'),
'email' => $request->get('email'),
'image' => $filename
), function($message) use($filename)
{
$message->from('[email protected]');
$message->to('[email protected]', 'Admin')->subject('Example Text');
$message->attach('/'.$filename);
});
usually i did this for uploading picture.
//Request File
$file = $request->file('image');
//Destination
$destination = public_path() . '/img/';
//Define the name
$name= "image";
//Get file extension
$extension = $file->getClientOriginalExtension();
//join the name you set with the extension
$filename = $name . '.' . $extension;
//after that move the file to the directory
$file->move($destination, $filename);
If you want to show the image don't forget use (instead of returning its name)
<img src="foo/bar" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With