Create a contact form in Laravel

New Chat

onoja.stanleyugo@gmail.com

Write code for contact using Laravel

To create a contact form in Laravel, you will need to follow these steps:

  1. Create a route for the contact form. In your routes/web.php file, add the following route:
Copy codeRoute::get('/contact', 'ContactController@create');

This route will map to the create method of your ContactController.

  1. Create a controller for the contact form. In your terminal, run the following command to create a new controller:
Copy codephp artisan make:controller ContactController

This will create a new ContactController.php file in your app/Http/Controllers directory.

  1. Define the create method in your ContactController. This method will be responsible for rendering the contact form view. Add the following code to your ContactController:
Copy codepublic function create()
{
    return view('contact.create');
}
  1. Create a view for the contact form. In your resources/views directory, create a new folder called contact and inside it create a new file called create.blade.php. This file will contain the HTML code for your contact form.
  2. Add a form to the contact view. In your create.blade.php file, add the following HTML code for a basic contact form:
Copy code<form method="POST" action="/contact">
  @csrf
  <div class="form-group">
    <label for="name">Name</label>
    <input type="text" class="form-control" id="name" name="name" required>
  </div>
  <div class="form-group">
    <label for="email">Email</label>
    <input type="email" class="form-control" id="email" name="email" required>
  </div>
  <div class="form-group">
    <label for="message">Message</label>
    <textarea class="form-control" id="message" name="message" rows="3" required></textarea>
  </div>
  <button type="submit" class="btn btn-primary">Send</button>
</form>

This form has three fields: name, email, and message, as well as a submit button. It also includes a @csrf directive, which is required to protect against cross-site request forgery attacks.

  1. Create a route to handle the form submission. In your routes/web.php file, add the following route to handle the form submission:
Copy codeRoute::post('/contact', 'ContactController@store');

This route will map to the store method of your ContactController.

  1. Define the store method in your ContactController. This method will be responsible for handling the form submission and sending the email. Add the following code to your ContactController:
Copy codeuse Illuminate\Support\Facades\Mail;

public function store(Request $request)
{
    $data = $request->validate([
        'name' => 'required',
        'email' => 'required|email',
        'message' => 'required',
    ]);

    Mail::send('emails.contact', $data, function($message
onoja.stanleyugo@gmail.com

How to set up email in Laravel

To set up email in Laravel, you will need to follow these steps:

  1. Configure your email settings in the .env file. Laravel uses the SwiftMailer library to send emails, and you will need to configure the following options in your .env file:
Copy codeMAIL_DRIVER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls

Replace smtp.example.com with the hostname of your email provider, and your_username and your_password with your email login credentials. If your email provider uses a different port or encryption method, you will need to adjust those settings as well.

  1. Test your email configuration. To test your email configuration, you can use the built-in mail:send Artisan command. In your terminal, run the following command:
Copy codephp artisan make:mail TestEmail

This will create a new TestEmail class in your app/Mail directory. Open the TestEmail.php file and add the following code:

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

class TestEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.test')
                    ->subject('Test Email');
    }
}

Next, create a new view file for the email in resources/views/emails/test.blade.php with the following content:

Copy code<h1>Test Email</h1>
<p>This is a test email sent from Laravel.</p>

Finally, in your terminal, run the following command to send the test email:

Copy codephp artisan mail:send "Test Email"

If everything is configured correctly, you should receive an email with the subject “Test Email” and the message “This is a test email sent from Laravel.”

  1. Send emails from your application. To send emails from your application, you can use the Mail facade. Here is an example of how to send a simple email:
Copy codeuse Illuminate\Support\Facades\Mail;

Mail::to('recipient@example.com')->send(new TestEmail());

You can also customize the email by setting the subject, sender, and other options using the $message variable:

Copy codeMail::send(['text' => 'emails.test'], $data, function($message) use ($request)
{
    $message->to('recipient@example.com', 'Recipient')
            ->subject('Test Email')
            ->from('sender@example.com', 'Sender');
});

For more advanced email functionality, you can refer to the Laravel documentation on sending mail.

Leave a comment