WEB DEVELOPER SITE
TUTORIALS HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Laravel - Sending Email


Sending Email : Laravel utilizes free component rich library SwiftMailer to send messages. Quick Mailer library work is simple we can undoubtedly send messages without an excessive number of hassles.The email formats are same similarly as views.which implies you can utilize the Blade punctuation and infuse information into your layouts.

Syntax void send(string|array $view, exhibit $data, Closure|string $callback)
Parameters
  • $view(string|array) – name of the view that contains email message

  • $data(array) – array of information to go to view

  • $callback – a Closure callback which gets a message occurrence, enabling you to tweak the beneficiaries, subject, and different parts of the mail message

Returns nothing
Description Sends email.
Mail can be sent as HTML or content. You can demonstrate the sort of mail that you need to send in the main contention by passing an exhibit as appeared as follows. The default type is HTML. On the off chance that you need to send plain content mail, at that point utilize the accompanying language structure.

Syntax

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

In this grammar, the primary contention takes a cluster. Use message as the key name of the view as estimation of the key.

Example

Step 1 − We will presently send an email from Gmail account and for that you have to arrange your Gmail account in Laravel condition record -.env document. Empower 2-step check in your Gmail account and make an application explicit secret key pursued by evolving the .env parameters as appeared/p>

.env

MAIL_DRIVER = smtp 

MAIL_HOST = smtp.gmail.com 

MAIL_PORT = 587 

MAIL_USERNAME = your-gmail-username 

MAIL_PASSWORD = your-application-explicit secret phrase 

MAIL_ENCRYPTION = tls 

Step 3 − Create a controller called MailController by executing the accompanying command.

php craftsman make:controller MailController - plain 

Step 4 − After fruitful execution, you will get the accompanying yield −

MailController

Step 5 − Copy the accompanying code in

app/Http/Controllers/MailController.php file.

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 expands Controller { 

open capacity basic_email(){ 

$data = array('name'=>"Virat Gandhi"); 

Mail::send(['text'=>'mail'], $data, function($message) { 

$message->to('abc@gmail.com', 'Instructional exercises Point')- >subject 

('Laravel Basic Testing Mail'); 

$message->from('xyz@gmail.com','Virat Gandhi'); 

}); 

reverberation "Fundamental Email Sent. Check your inbox."; 

} 

open capacity html_email(){ 

$data = array('name'=>"Virat Gandhi"); 

Mail::send('mail', $data, function($message) { 

$message->to('abc@gmail.com', 'Instructional exercises Point')- >subject 

('Laravel HTML Testing Mail'); 

$message->from('xyz@gmail.com','Virat Gandhi'); 

}); 

reverberation "HTML Email Sent. Check your inbox."; 

} 

open capacity attachment_email(){ 

$data = array('name'=>"Virat Gandhi"); 

Mail::send('mail', $data, function($message) { 

$message->to('abc@gmail.com', 'Instructional exercises Point')- >subject 

('Laravel Testing Mail with Attachment'); 

$message->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'); 

}); 

reverberation "Email Sent with connection. Check your inbox."; 

} 

} 

Step 2 − After changing the .env record execute the underneath two directions to clear the store and restart the Laravel server.

php craftsman config:cache 

Step 3 − Create a controller called MailController by executing the accompanying command.

php craftsman make:controller MailController - plain 

Step 4 − After fruitful execution, you will get the accompanying yield −

MailController

Step 5 − Copy the accompanying code in

app/Http/Controllers/MailController.php file.

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 expands Controller { 

open capacity basic_email(){ 

$data = array('name'=>"Laravel"); 

Mail::send(['text'=>'mail'], $data, function($message) { 

$message->to('abc@gmail.com', 'Instructional exercises Point')- >subject 

('Laravel Basic Testing Mail'); 

$message->from('xyz@gmail.com','Laravel'); 

}); 

reverberation "Essential Email Sent. Check your inbox."; 

} 

open capacity html_email(){ 

$data = array('name'=>"Laravel"); 

Mail::send('mail', $data, function($message) { 

$message->to('abc@gmail.com', 'WeLOOKUPS')- >subject 

('Laravel HTML Testing Mail'); 

$message->from('xyz@gmail.com','Laravel'); 

}); 

reverberation "HTML Email Sent. Check your inbox."; 

} 

open capacity attachment_email(){ 

$data = array('name'=>"Laravel"); 

Mail::send('mail', $data, function($message) { 

$message->to('abc@gmail.com', 'WeLOOKUPS')- >subject 

('Laravel Testing Mail with Attachment'); 

$message->attach('C:\laravel-master\laravel\public\uploads\image.png'); 

$message->attach('C:\laravel-master\laravel\public\uploads\test.txt'); 

$message->from('xyz@gmail.com','Laravel'); 

}); 

reverberation "Email Sent with connection. Check your inbox."; 

} 

} 

Step 6 − Copy the accompanying code in resources/sees/mail.blade.php file.

resources/sees/mail.blade.php

<h1>Hi, {{ $name }}</h1> 

l<p>Sending Mail from Laravel.</p> 

Step 7 − Add the accompanying lines in app/Http/routes.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 accompanying URL to test fundamental email.

http://localhost:8000/sendbasicemail 

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

Basic Email Sent

Step 10 − Visit the accompanying URL to test the HTML email.

http://localhost:8000/sendhtmlemail 

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

HTML Email

Step 12 − Visit the accompanying URL to test the HTML email with attachment.

http://localhost:8000/sendattachmentemail