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:abc@xyz.com";
    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 = "bordadoscreativos.02@gmail.com";
    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.

ayy_chemixd
  • 113
  • 10

2 Answers2

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

Ranndy360
  • 9
  • 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 = "bordadoscreativos.02@gmail.com";
        $headers = "From:abc@xyz.com";
        $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
    ];
 }