0

I have the following code in html, a simple email form:

                <form class="main_form" method="post" action="sendmail.php">
                    <div class="row">
                     
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                            <input class="form-control" placeholder="title" type="text" name="Title">
                        </div>
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                            <input class="form-control" placeholder="Email" type="text" name="Email">
                        </div>
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                            <textarea class="textarea" placeholder="message" type="text" name="Message"></textarea>
                        </div>
                        <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                            <button class="send" >send</button>
                        </div>
                    </div>
                </form>

And this is the script I want to run everytime you hit "send" (sendmail.php)

<?php
  if (isset($_POST['submitted_form'])){
    $subject = $_POST['Title'];
    $message = "Email : $_POST['Email'] Message: $_POST['Message']";
    $headers = "From:[email protected]";
    send_mail($subject,$message,$headers);
  }
function send_mail( $subject, $message, $headers){
    // https://stackoverflow.com/questions/5335273/how-can-i-send-an-email-using-php
    $to = "[email protected]";
    if(mail($to, $subject, $message, $headers)){
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
}
?>

Now my problem is that everytime I hit send, the URL changes to http://127.0.0.1:8000/sendmail.php and it throws a 404. what should I do? Create a controller for sendmail.php? I already tried creating a route inside web.php for sendmail.php but it still doesn't work.

3
  • 1
    instead of creating sendmail.php .create a method in controller Commented May 29, 2021 at 16:53
  • @JohnLobo and how do I redirect my html code to that controller? Commented May 29, 2021 at 16:57
  • 1
    you need to create routes .just learn basics of laravel first then work on it.so it help you to solve most of the issues yourself. If you google it lots of laravel resources available Commented May 29, 2021 at 16:59

2 Answers 2

1

Create send_mail.blade.php in resource/view path

<form class="main_form" method="post" action="/send_mail">
      @csrf
                <div class="row">
                 
                    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                        <input class="form-control" placeholder="title" type="text" name="Title">
                    </div>
                    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                        <input class="form-control" placeholder="Email" type="text" name="Email">
                    </div>
                    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                        <textarea class="textarea" placeholder="message" type="text" name="Message"></textarea>
                    </div>
                    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12">
                        <button class="send" >send</button>
                    </div>
                </div>
            </form>

Then create new class SendMailController.php in app\Http\Controllers path

namespace App\Http\Controllers;
use App\Models\SendMail;
use Illuminate\Http\Request;

class BankCashController extends Controller
{
    public function send_mail(Request $request) {
        $to = "[email protected]";
        $headers = "From:[email protected]";
        $subject = $request->get('Subject');
        $message = $request->get('Email') . "</br>" . $request->get('Message');

        if(mail($to, $subject, $message, $headers)){
            echo "Message sent successfully...";
        } else {
            echo "Message could not be sent...";
        }
    }
}

Add this route in routes\web.php

Route::post('/send_mail', 'app\Http\Controllers\SendMailControllere@send_mail');

You need alos create SendMail.php model in app\Models path

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class SendMail extends Model
{
    protected $guard_name = 'web';

    protected $fillable = [
        'title',
        'email',
        'message',
        'to',
        ... //add all columns of your table
    ];
 }
Sign up to request clarification or add additional context in comments.

3 Comments

and what would the model contain? the meat of the code is already inside the controller. just that and I'll accept your answer
also it returns a 419 error. I think its because its missing a CSRF token in the html form. I'll research on it and try to add later
Oh, Yes. You should add @csrf inside form block.
1

You have a project structure issue here, if you are using Laravel you have to separate in MVC pattern, it means that all logic have to be in controller not in your views.

Besides if you want to use php code in your views you could use blade, blade is usefull for conditions, bucles, etc etc.

Lear more about how laravel project structure and blade Doc Laravel Blade

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.