Categories
Uncategorized

Laravel – Send email from any controller action

Laravel send email from any controllers. In this article, I am going to discuss the Mail API in the Laravel web framework. Laravel provides a clean, simple API over the popular SwiftMailer library with drivers for SMTP, Mailgun, sendmail etc. you are allowing to quickly get started sending mail.

The below are some easy steps to follow, and you will be able to send email in Laravel controller.

  • Install Laravel.
  • Create/Update Controller
  • Define a Controller Route.
  • SMTP configure in the .env file.
  • Create a Mailable class.
  • Create a Email Template.

Install Laravel.

Run following command to create new Laravel project.

Create/Update controller

Create new controller or update already defined method in your existing controller. Here I have create a new controller using below command.

After successfully create new controller you can update below code in your file.
app\Http\Controllers\EmailSendController.php

Define a route to send an email.

Now create a route in your web.php file to sending email to the user which you want.

Setting up SMTP details in .env file

We will use gmail SMTP to configure the settings in the .env file.

Create a Mailable class

Open your terminal and go to project root directory and type the following command.

So, it will create a file inside App\Mail\ContactUsMail.php. Now, this class catch form submitted data, and pass data to email template.

Create a email Template file in resources/views/emails/contact-us.blade.php

Now, you need to create a blade file inside the views/emails folder where you show email content and send form in laravel controller.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please SHARE this article