Laravel - Sending Email

Laravel - Sending Email

Laravel uses the free, feature rich SwiftMailer library to send emails. Using the library function, we can easily send emails without much hassle. Email templates are loaded just like views, which means you can use the Blade syntax and inject data into your templates.

The following table shows the syntax and attributes of the send function −

Syntax void send(string | $view array, $data array, Closure | $callback string)
parameters
  • $view(string|array) - The name of the view that contains the email message

  • $ data (array) - array of data to pass to view

  • $callback is a Closure callback that receives an instance of the message, allowing you to customize the recipients, subject, and other aspects of the mail message.

returns nothing
Description Sends email.

$view(string|array) - The name of the view that contains the email message

$ data (array) - array of data to pass to view

$callback is a Closure callback that receives an instance of the message, allowing you to customize the recipients, subject, and other aspects of the mail message.

In the third argument, $callback closure received a message instance and with this instance we can also call the following functions and change the message as shown below.

  • $message → subject("Welcome to the tutorials item");
  • $message → from('email@example.com','Mr. Example');
  • $message -> to('email@example.com','Mr. Example');

Some of the less common methods include −

  • $message → sender('email@example.com','Mr. Example');
  • $message -> returnPath('email@example.com');
  • $message → cc('email@example.com', 'Mr. Example');
  • $message → bcc('email@example.com', 'Mr. Example');
  • $message -> replyTo('email@example.com','Mr. Example');
  • $message -> priority(2);

To attach or embed files, you can use the following methods −

  • $message -> attach('path/to/attachment.txt');
  • $message -> embed('path/to/attachment.jpg');

Mail can be sent as HTML or text. You can specify the type of mail you want to send in the first argument by passing in an array, as shown below. The default type is HTML. If you want to send a text message, use the following syntax.

Syntax

Mail::send(['text'=>'text.view'], $data, $callback);

In this syntax, the first argument takes an array. Use text as the view key name as the key value.

example

Step 1 − We will now send an email from a Gmail account, for which you need to set up your Gmail account in the Laravel environment file .env file . Enable 2-Step Verification on your Gmail account and create a custom app password, then edit the .env settings as shown below.

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT = 587
MAIL_USERNAME = your-gmail-username
MAIL_PASSWORD = your-application-specific-password
MAIL_ENCRYPTION=tls

Step 2 − After modifying the .env file, run the two commands below to clear the cache and restart the Laravel server.

php artisan config:cache

Step 3 − Create a controller named MailController by running the following command.

php artisan make:controller MailController --plain

Step 4 − Upon successful execution, you will get the following output −

Step 5 - Copy the following code into

The file is app/Http/Controllers/MailController.php .

app/Http/Controllers/MailController.php

<? php

namespace App \Http\Controllers ; use Illuminate \Http\Request ; use Mail ; 
 
 

use App \Http\Requests ; use App \Http\Controllers\Controller ; 

class MailController extends Controller { public function basic_email () { 
      $data = array ( 'name' => "Virat Gandhi" );    
     
   
      Mail :: send ([ 'text' => 'mail' ], $data , function ( $message ) { 
         $message -> to ( 'abc@gmail.com' , 'Tutorials Point' )-> subject
             ( 'Laravel Basic Testing Mail' ); 
         $message -> from ( 'xyz@gmail.com' , 'Virat Gandhi' ); }); 
      echo "Basic Email Sent. Check your inbox." ;   
      
   
    () { 
      $data = array ( 'name' => "Virat Gandhi" ); Mail :: send ( 'mail' , $data , function ( $message ) { 
         $message -> to ( 'abc@gmail.com' , 'Tutorials Point' )-> subject
             ( 'Laravel HTML Testing Mail' ); 
         $ message -> from ( 'xyz@gmail.com' , 'Virat Gandhi' ); }); 
         
      "HTML Email Sent. Check your inbox." ; } public function attachment_email () { 
      $data = array ( 'name' => "Virat Gandhi" ); Mail :: send ( 'mail' , $data , function ( $message ) { 
         $message -> to ( 'abc@gmail.com' , 'Tutorials Point' )-> subject
             ( 'Laravel Testing Mail with Attachment' );
   
     
         attach ( 'C:\laravel-master\laravel\public\uploads\image.png' ); 
         $message -> attach ( 'C:\laravel-master\laravel\public\uploads\test.txt' ); 
         $message -> from ( 'xyz@gmail.com' , 'Virat Gandhi' ); }); 
      echo "Email Sent with attachment. Check your inbox." ; } }
      
   

Step 6 - Copy the following code to the resources/views/mail.blade.php file .

resources/views/mail.blade.php

<h1>Hi, {{ $name }}</h1>
l<p>Sending Mail from Laravel.</p>

Step 7 - Add the following lines to app/Http/rout.php.

app/http/routes.php

Route::get('sendbasicemail','MailController@basic_email');
Route::get('sendhtmlemail','MailController@html_email');
Route::get('sendattachmentemail','MailController@attachment_email');

Step 8 − Visit the following URL to check your primary email.

http://localhost:8000/sendbasicemail

Step 9 − The output screen will look something like this. Check your inbox to see the main email output.

Step 10 − Visit the following URL to check the HTML email.

http://localhost:8000/sendhtmlemail

Step 11 − The output screen will look something like this. Check your inbox to see the HTML output of the email.

Step 12 − Visit the following URL to inspect the HTML email with attachment.

http://localhost:8000/sendattachmentemail

Step 13 - You can see the following output

Note. In the MailController.php file, the email address in the from method must be an email address from which you can send an email address. Typically, this should be the email address configured on your server.