0
votes

I have verified emails are sending via mailgun with a public function mail using a get request with no data on the page. Trying to send an email from a contact page with a POST request simply refreshes the page with no errors but email does not send.

I have set the .env and config.mail / config.services and successfully send mail using a get request in web.php.

First I used Terminal php artisan make:mail. Then I created two routes for a contact page (GET / POST), created a contact view (works), send the data to PagesController.php, run a validation of the three inputs, pass the data to Mail::send. On click of submit button the page refreshes with no errors (validation not working) and email doesn't send.

PagesController.php:

Namespace(

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use App\Mail\Welcome;
use Mail;

class PagesController extends Controller {

public function getContact(){
   return view('pages/contact');
    }

public function postContact(Request $request){
  $this->validate($request,[
     'email' => 'required|email',
     'subject' => 'min:3',
     'message' => 'min: 3'
  ]);

$data = array(
    'email' => $request->email,
    'subject' => $request->subject,
    'bodyMessage' => $request->message
  );

\Mail::send('email.Test', $data, function($message) use ($data){
    $message->from($data['email']);
    $message->to('[email protected]');
    $message->subject($data['subject']);
 });

}

Web.php:

Route::get('contact', 'PagesController@getContact');
Route::post('contact', 'PagesController@postContact');

Test.blade.php:

<html>

 <h1> Email from: {{ $email }}</h1>

 <p> {{ $subject }} </p>
 <br><br>
 <hr>
 <p>{{ $bodyMessage}}</p>

contact.blade.php:

@extends('main')

@section('title', '| Contact')

@section('content')


  <div class="row">
    <div class="col-md-12">
      <h1>Contact Me</h1>
      <hr>
      <form acion="{{ url('contact') }}">
        {{ csrf_field() }}
        <div class="form-group">
          <label name="email">Email:</label>
          <input id="email" name="email" class="form-control">
        </div>

        <div class="form-group">
          <label name="subject">Subject:</label>
          <input id="subject" name="subject" class="form-control">
        </div>

        <div class="form-group">
          <label name="message">Message:</label>
          <textarea id="message" name="message" class="form-             control">Type your message here...</textarea>
        </div>

        <input type="submit" value="Send message" class="btn btn-success">

      </form>
    </div>
  </div>

@endsection

All I want to do is pull the information from the form and send it via email to a set address. I have been working on this for over week and now that I am not getting errors I have no idea what to do.

2

2 Answers

0
votes

You can add this above the form in your blade file. You would be able to see the validation errors if any :

@if ($errors->any())
     @foreach ($errors->all() as $error)
         <div>{{$error}}</div>
     @endforeach
 @endif

Also change :

<form acion="{{ url('contact') }}">

To

<form action="{{ url('contact') }}" method="POST">
-1
votes

put some method on your form : method="POST" because once you will submit it, it will route to the GET route not the POST route since you did not declare a method on your form