Laravel: How to Send email in laravel?

To send email in Laravel, you can use the built-in email sending functionality provided by Laravel's Mail class. Here's a step-by-step guide on how to send an email in a Laravel application:

  1. Configure Mail Settings:

    First, you need to configure your email settings in the .env file of your Laravel application. Open the .env file and set the following email configuration variables:

    dotenv
  • MAIL_DRIVER=smtp MAIL_HOST=your-smtp-server.com MAIL_PORT=587 MAIL_USERNAME=your-email@example.com MAIL_PASSWORD=your-email-password MAIL_ENCRYPTION=tls

    Replace the placeholders with your actual email server information, including SMTP server, port, email address, and password.

  • Create an Email Template (Optional):

    You can create a Blade view file that serves as your email template. This view file will be used to format the email content. For example, create a view file named email.blade.php in the resources/views directory.

    html
  • <!DOCTYPE html> <html> <head> <title>Your Email Subject</title> </head> <body> <p>Hello, {{ $name }}</p> <p>This is a test email.</p> </body> </html>
  • Create a Mailable Class:

    In Laravel, you can create a Mailable class that defines the email message. You can use the Artisan command-line tool to generate a Mailable class:

    bash
  • php artisan make:mail MyTestMail

    This will create a Mailable class in the app/Mail directory.

  • Customize the Mailable Class:

    Open the generated Mailable class (e.g., MyTestMail.php) in the app/Mail directory. In the build method, you can customize the email subject, recipient, and any other email-related information. You can also pass data to your email template (if you created one) using the with method.

    php
  • use Illuminate\Mail\Mailable; class MyTestMail extends Mailable { public function build() { return $this->subject('Your Email Subject') ->view('email') ->with(['name' => 'John']); } }
  • Sending the Email:

    You can send the email from a controller, route, or any other part of your application. To send the email, use the Mail facade. For example, you can send it from a controller method:

    php
  • use App\Mail\MyTestMail; use Illuminate\Support\Facades\Mail; public function sendEmail() { Mail::to('recipient@example.com')->send(new MyTestMail()); return 'Email sent successfully!'; }

    Replace 'recipient@example.com' with the email address where you want to send the email.

  • Testing the Email:

    To test the email functionality, you can call the sendEmail method by accessing a URL or executing a controller action. For example, if you defined a route:

    php
    1. Route::get('/send-email', 'YourControllerName@sendEmail');

      You can access this route in your browser or via a tool like Postman to trigger the email sending process.

    That's it! You've successfully set up email sending in Laravel. When you access the appropriate route or controller action, Laravel will send the email using the configured SMTP settings and the Mailable class you created.

    Comments