Laravel 5.3 tutorial step by step - Laravel 5.3 can handle email functions, that we can send email using costum SMTP like smtp.gmail.com, Laravel uses free feature-rich library "SwiftMailer" to send emails.
At the previews lessons about Laravel 5.3 we has learned How to Create File Upload in laravel 5.3 and How to use Validation in Laravel 5.3, just learn it before try this one.
Send Email using Gmail SMTP in laravel 5.3
now, this lessons we will create simple apps that we can send email using smtp gmail.com, by following this syntax:
Mail::send(['text'=>'text.view'], $data, $callback);
First, we need to configurate our gmail account to Laravel project, that is on ".ENV" file, costum and configure with your gmail account :
.env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls
Next, we need to clear our cache and restar Laravel Server by following this command :
Clear cache
php artisan config:cache
Next, we will create new controller to send email with Laravel by following this command :
Create new Controller
php artisan make:controller MailController
next, we will create three function in MailController.php
MailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
// we will use Mail namespace
use Mail;
class MailController extends Controller
{
// first, we create function for send Basics email
public function basic_email(){
$data=['name'=>'Harison matondang'];
Mail::send(['text'=>'mail'], $data, function($message){
$message->to('harisonmatondang@gmail.com','Harison Matondang')->subject('Send Mail from Laravel with Basics Email');
$message->from('nabilaauliaputri29@gmail.com','Nabila');
});
echo 'Basics Email was sent!';
}
//create new function to send HTML email
public function html_email(){
$data=['name'=>'Harison matondang'];
Mail::send(['text'=>'mail'], $data, function($message){
$message->to('harisonmatondang@gmail.com','Harison Matondang')->subject('Send Mail from Laravel with HTML Email');
$message->from('nabilaauliaputri29@gmail.com','Nabila');
});
echo 'HTML Email was sent!';
}
//create new function to send mail with attachment Mail
public function attachment_email(){
$data=['name'=>'Harison matondang'];
Mail::send(['text'=>'mail'], $data, function($message){
$message->to('harisonmatondang@gmail.com','Harison Matondang')->subject('Send Mail from Laravel with HTML Email');
// add attach here
// i have a image file on Laravel project
$message->attach('C:serverhtdocshckrcompublicuploadsharison.jpg');
$message->attach('C:serverhtdocshckrcompublicuploadssector-code.jpg');
$message->from('nabilaauliaputri29@gmail.com','Nabila');
});
echo 'HTML Email was sent!';
}
}
Next, create new view, that folder stored on resources/views and rename it with "mail.blade.php"
mail.blade.php
Hallo, {{ $name }}
i send a mail
Next create new routes
web.php
Route::get('/basicemail', 'MailController@basic_email');
Route::get('/htmlemail', 'MailController@html_email');
Route::get('/attachemail', 'MailController@attachment_email');
After finished, try to access your project with browser by following this URL :
http://localhost:8080/basicemail
http://localhost:8080/htmlemail
http://localhost:8080/attachemail
If you got some error like about Security by your gmail account, just setting and Allow less secure applications, by follwing this URL https://myaccount.google.com/security#activity and you will solved your problem.
Video tutorial Send Email with laravel
See you Next Lessons ...
Không có nhận xét nào:
Đăng nhận xét