I'm working on a project in Laravel 8. I make a contact form that will be sent to my email address and contains name, email, message and attachment. Name, email and message are required but no attachment. When I send the form completely filled in (name, email, message and attachment) everything works fine and the message comes to my email address (attachment too) But if I fill in only the name, email, message and attachment I leave blank it will print an error:
Call to a member function getRealPath() on null.
I need the message to be sent without an attached attachment.
Code:
contact_us_blade.php:
<form action="contact_us" method="POST" enctype="multipart/form-data">
@csrf
<input type="text" name="name">
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<input type="text" name="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<textarea name="message"> </textarea>
@error('message')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<input type="file" name="image">
<br>
<input type="submit" value="odosli">
</form>
contact_email.blade.php
<h2>Hello Admin,</h2>
<br><b>Name:</b> {{ $name }}
<br><b>Email:</b> {{ $email }}
<br><b>Message:</b> {{ $messages }}
EmailController.php:
public function index(){
return view('contact.contact_us');
}
public function send(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
'image' => 'sometimes|required'
]);
$data = [
'name' => $request->name,
'email' => $request->email,
'messages' => $request->message,
'image' => $request->file('image')
];
$contact = new Lar_Contact;
$contact->name = $request->name;
$contact->email = $request->email;
$contact->message = $request->message;
$contact->save();
$to = '*******@gmail.com';
\Mail::to($to)->send(new \App\Mail\Mail($data));
return back()->with('success', 'Thanks for your message!');
}
Mail.php:
use Queueable, SerializesModels;
protected $data;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$name = $this->data['name'];
$email = $this->data['email'];
$messages = $this->data['messages'];
return $this->subject ('Subject email')
->view('contact.contact_email', compact('name'))
->view('contact.contact_email', compact('email'))
->view('contact.contact_email', compact('messages'))
->attach($this->data['image']->getRealPath(),[
'as' => $this->data['image']->getClientOriginalName(),
]);
}
Conditionally attach the image?
$mail = $this->subject ('Subject email')
->view('contact.contact_email', compact('name'))
->view('contact.contact_email', compact('email'))
->view('contact.contact_email', compact('messages'))
if (isset($this->data['image']) && !is_null($this->data['image'])) {
$mail->attach($this->data['image']->getRealPath(),[
'as' => $this->data['image']->getClientOriginalName(),
]);
}
return $mail;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.