Home > Enterprise >  Laravel 9.x After submitting the form Mailgun returns not defined Error. [Email couldn't delive
Laravel 9.x After submitting the form Mailgun returns not defined Error. [Email couldn't delive

Time:04-10

Trying to send data with input form. User sends information such as name, email, subject and the message. This form is getting used for reporting issues to do admin. When submit the form, returns Mailgun not defined error.

new to laravel and mvc.

The Error

.env

MAIL_MAILER=Mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587 
[email protected]
MAIL_PASSWORD=myPassword

config/mail.php

'default' => env('MAIL_MAILER', 'smtp'),

    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
        ],
        
    ],

        'mailgun' => [
            'transport' => 'mailgun',
        ],

        'failover' => 
        [
            'transport' => 'failover',
            'mailers' => [
                'smtp',
                'log',
            ],
        ],

app/Mail/contacter.php

namespace App\Mail;

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

class contacter extends Mailable
{
    use Queueable, SerializesModels;

    public $emailData;
  
    public function __construct($emailData)
    {
        $this->mailData = $emailData;
      
    }
    public function build()
    {
        return $this->subject('Message')
                    ->view('mail');
    }
}

ContactUsFormController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use app\Models\Contact;
use app\Mail\contacter;
use Illuminate\Support\Facades\Mail;


class ContactUsFormController extends Controller
{
    public function createForm(Request $req){
        return view('contact');
    }
    public function ContactUsForm(Request $req){
                //form validation 
                $this->validate($req,[
                    'name' => 'required',
                    'email' => 'required | email',
                    'subject' => 'required',
                    'message' => 'required'
                ]);
                $email = [
                    'name' => $req->get('name'),
                    'email' => $req->get('email'),
                    'subject' => $req->get('subject'),
                    'message' => $req->get('message')
                ];

              Mail::to(env('ADMIN'))->send(new contacter($email));

            return back()->with('success', 'We have recieved your message and will reply to ' . $email['email'] . ' email address!');


   }
}

web.php

Route::get('/contact', [ContactUsFormController::class, 'createForm']);
Route::post('/contact', [ContactUsFormController::class, 'ContactUsForm'])->name('contact.store');

mail.blade

    <h1>About : {{ $emailData['subject'] }}</h1>
    <p>From : {{ $emailData['name'] }}</p>
    <p>Reply To : {{$emailData['email']}}</p> <br /><br /><br />
    <p>{{$emailData['message']}}</p>
     
    <br />
    <p>END</p>

CodePudding user response:

from your .env it looks like you using your mailgun smtp credentials

then you should define your driver as SMTP

MAIL_MAILER=smtp

I hope it's helpful

CodePudding user response:

change in contracter.php file

//public $emailData;
// to

public $mailData;

public function __construct($emailData)
{
    $this->mailData = $emailData;
}
  • Related