发送不需要附件的邮件 Laravel 8
Sending mail with no required attachment Laravel 8
我正在 Laravel 8 做一个项目。我制作了一个联系表格,该表格将发送到我的电子邮件地址,其中包含姓名、电子邮件、消息和附件。姓名、电子邮件和消息是必需的,但没有附件。
当我发送完整填写的表格(姓名、电子邮件、消息和附件)时,一切正常,消息到达我的电子邮件地址(附件也是)
但是如果我只填写姓名、电子邮件、消息和附件我留空它会打印错误:
Call to a member function getRealPath() on null.
我需要在没有附加附件的情况下发送邮件。
代码:
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(),
]);
}
有条件附上图片?
$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;
我正在 Laravel 8 做一个项目。我制作了一个联系表格,该表格将发送到我的电子邮件地址,其中包含姓名、电子邮件、消息和附件。姓名、电子邮件和消息是必需的,但没有附件。 当我发送完整填写的表格(姓名、电子邮件、消息和附件)时,一切正常,消息到达我的电子邮件地址(附件也是) 但是如果我只填写姓名、电子邮件、消息和附件我留空它会打印错误:
Call to a member function getRealPath() on null.
我需要在没有附加附件的情况下发送邮件。
代码:
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(),
]);
}
有条件附上图片?
$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;