cloud soft
cloud soft

Reputation: 195

how to solve error from sending email from contact form

I am trying to send mail from my contact form. But I am getting error.

contact.blade.php is:

<form method="post" action="{{ URL('send') }}">
  {{csrf_field()}}
    <table align="center" width="400">
   <tr>
     <td><strong>Full Name</strong></td>
    <td><input type="text" name="name" required="required" /></td>
      </tr>
       <tr>
      <td><strong>Contact No.</strong></td>
     <td><input type="text" name="mobno" required="required" /></td>
      </tr>
      <tr>
       <td><strong>Email ID</strong></td>
        <td><input type="text" name="email" required="required" /></td>
        </tr>
      <tr>
        <td><strong>Message</strong></td>
        <td><textarea name="msg" cols="30" rows="3" required="required"></textarea></td>
      </tr>
        <tr>
           <td>&nbsp;</td>
           <td><input type="submit" name="submit" /></td>
         </tr>
       </table>
     </form>

web.php is:

Route::POST('send', 'ContactController@send');

ContactController.php is:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\officeholder;
use App\Mail\SendMail;
use Mail;

class ContactController extends Controller
{

public function send()
{
    Mail::send(new sendMail());
}
}

**I have created SendMail.php using

php artisan make:mail SendMail

by my cmd and then App\Mail\SendMail.php is created.**

SendMail.php is:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\request;

class SendMail extends Mailable
{
use Queueable, SerializesModels;


public function __construct()
{
    //
}


public function build(Request $request)
{
    return $this->view('contact',['msg'=>$request->msg])->to('[email protected]');
}
}

But I got error after click button on form.

1/1) FatalErrorException Class 'App\Http\Controllers\sendMail' not found

in ContactController.php line 18

Upvotes: 0

Views: 748

Answers (2)

Vasyl Zhuryk
Vasyl Zhuryk

Reputation: 1248

Change your code:

Mail::send(new sendMail()); 

to

Mail::send(new SendMail());

and

use App\Mail\SendMail;

to

use \App\Mail\SendMail;

Update:

try this:

public function build(Request $request)
{
    return $this
             ->view('contact')
             ->with(['msg' => $request->msg])
             ->to('[email protected]');
}

You need to set variables with function with()

Upvotes: 2

Shailendra Gupta
Shailendra Gupta

Reputation: 1128

use like this

<?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\officeholder;
    use Mail;
    use App\Mail\SendMail;

    class ContactController extends Controller
    {

          public function send()
          {
              Mail::send(new SendMail());
          }
    }

Upvotes: 0

Related Questions