LARAVEL

Sending email


Sending Email

After validating the form data, you can send an email to the specified recipient. Laravel provides a simple and expressive API for sending emails using its built-in Mail class.

 

Configure Mail Driver

First, ensure that your Laravel application is properly configured to send emails. Open the .env file in your Laravel project and configure your mail settings, such as the mail driver, mail host, username, password, etc. For example:

MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your-email@example.com
MAIL_PASSWORD=your-email-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email@example.com
MAIL_FROM_NAME="${APP_NAME}"

 

Sending Email in Controller

In your controller (ContactFormController), import the Mail facade and use it to send an email after validating the form data. Here's an example of how you can send an email:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactFormEmail;

class ContactFormController extends Controller
{
    public function show()
    {
        return view('contact');
    }

    public function submit(Request $request)
    {
        // Validate the form data
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'message' => 'required|string',
        ]);

        // Send email
        Mail::to('your-email@example.com')->send(new ContactFormEmail($request->all()));

        return redirect('/contact')->with('success', 'Your message has been sent successfully!');
    }
}

 

Create Mail Class

Next, you need to create a mailable class that represents the email you want to send. You can use the make:mail Artisan command to generate a new mailable class. For example:

php artisan make:mail ContactFormEmail

This will create a new mailable class named ContactFormEmail in the app/Mail directory. Open the generated class and customize it to define the email content and any additional options, such as subject, sender, etc.

<?php

namespace App\Mail;

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

class ContactFormEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $formData;

    /**
     * Create a new message instance.
     *
     * @param array $formData
     * @return void
     */
    public function __construct(array $formData)
    {
        $this->formData = $formData;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('New Contact Form Submission')
                    ->view('emails.contact');
    }
}

 

Customize Email View

Create a Blade view file to define the email content. By default, Laravel will look for a view file with the same name as the mailable class (contact.blade.php in this case) in the resources/views/emails directory.

For example, you can create resources/views/emails/contact.blade.php and customize it to include the form data:

<!DOCTYPE html>
<html>
<head>
    <title>Contact Form Submission </title>
</head>
<body>
    <h1>New Contact Form Submission</h1>
    <p>Name: {{ $formData['name'] }}</p>
    <p>Email: {{ $formData['email'] }}</p>
    <p>Message: {{ $formData['message'] }}</p>
</body>
</html>

LARAVEL