Skip to content

Commit

Permalink
Laravel Mail Notification
Browse files Browse the repository at this point in the history
  • Loading branch information
subhadipghorui committed Nov 8, 2020
1 parent 20e97c5 commit da47bf5
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use App\Category;
use App\Http\Controllers\Controller;
use App\Mail\NewPost;
use App\Post;
use App\User;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
Expand Down Expand Up @@ -80,6 +83,13 @@ public function store(Request $request)
}
$post->save();

// Notification by mail
if($post->status){
$users = User::all();
foreach($users as $user){
Mail::to($user->email)->queue(new NewPost($post));
}
}
$tags = [];
$stingTags = array_map('trim', explode(',', $request->tags));
foreach ($stingTags as $tag) {
Expand Down Expand Up @@ -178,6 +188,13 @@ public function update(Request $request, $id)
$post->status = false;
}
$post->save();
// Notification by mail
if($post->status){
$users = User::all();
foreach($users as $user){
Mail::to($user->email)->queue(new NewPost($post));
}
}
// delete old tags
$post->tags()->delete();
$tags = [];
Expand Down
34 changes: 34 additions & 0 deletions app/Mail/NewPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class NewPost extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @return void
*/
public $post;
public function __construct($post)
{
$this->post = $post;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('You have a new post.')->view('mail.newpost');
}
}
36 changes: 36 additions & 0 deletions database/migrations/2020_11_08_065456_create_jobs_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('jobs');
}
}
106 changes: 106 additions & 0 deletions resources/views/mail/newpost.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>New Post Available</title>
<link
href="https://fonts.googleapis.com/css?family=Poppins:100,200,400,300,500,600,700"
rel="stylesheet"/>
<!--
CSS
============================================= -->
<link rel="stylesheet" href="{{asset('frontend/css/linearicons.css')}}" />
<link rel="stylesheet" href="{{asset('frontend/css/font-awesome.min.css')}}" />
<link rel="stylesheet" href="{{asset('frontend/css/bootstrap.css')}}" />
<link rel="stylesheet" href="{{asset('frontend/css/owl.carousel.css')}}" />
<link rel="stylesheet" href="{{asset('frontend/css/main.css')}}" />
<style>
.heading{
width: 100%;
/* background: rgb(245, 245, 245); */
display: flex;
justify-content: center;
padding: 20px 0;
margin-bottom: 1rem;
}
.mail-footer{
width: 100%;
background: rgb(6, 101, 156);
color: #fff;
padding: 20px 10px;
}
.mail-footer a{
color: #fff;
}
.mail-footer a:hover{
color: rgb(5, 247, 255);
}
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-6 heading">
<img src="http://localhost:8000/frontend/img/sitelogo.png" alt="logo" width="100px">
{{-- <h1>myiotlab</h1> --}}
</div>
</div>
<div class="row justify-content-center">
<div class="single-posts col-lg-6 col-sm-6">
<img class="img-fluid" src="{{asset('storage/post/'.$post->image)}}" alt="">
<div class="date mt-20 mb-20">{{$post->created_at->diffForHumans()}}</div>
<div class="detail">
<a href="{{route('post', $post->slug)}}"><h3 class="pb-20">{{$post->title}}</h3></a>
<div class="row d-flex">
<h4 class="ml-3">{{$post->category->name}}</h4>
<div class="col-lg-4 col-md-12 right-side d-flex justify-content-end ml-auto">
<div class="desc">
<h4>{{$post->user->name}}</h4>
<p>{{$post->created_at->diffForHumans()}}</p>
</div>
<div class="user-img">
<img src="{{asset('storage/user/'.$post->user->image)}}" alt="" width="50px"/>
</div>
</div>
</div>
<p>
{!!Str::limit($post->body, 300)!!}
</p>
<p class="footer pt-20">
<i class="fa fa-heart-o" aria-hidden="true"></i>
<a href="#">{{$post->likedUsers->count()}} Likes</a> <i class="ml-20 fa fa-comment-o" aria-hidden="true"></i> <a href="#">{{$post->comments->count()}} Comments</a>
</p>
</div>
<div class="justify-content-center d-flex">
<a class="text-uppercase primary-btn loadmore-btn mt-70 mb-60" href="{{route('post', $post->slug)}}">Read more</a>
</div>
</div>

</div>
<div class="row justify-content-center">
<div class="col-6 mail-footer">
<p>This email is from <a href="https://myiotlab.in">myiotlab.in</a></p>
<p>© 2020 myiotlab. All Rights Reserved</p>
</div>
</div>
</div>

{{-- JS --}}
<script src="{{asset('frontend/js/vendor/jquery-2.2.4.min.js')}}"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"
integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4"
crossorigin="anonymous"
></script>
<script src="{{asset('frontend/js/vendor/bootstrap.min.js')}}"></script>
<script src="{{asset('frontend/js/jquery.ajaxchimp.min.js')}}"></script>
<script src="{{asset('frontend/js/parallax.min.js')}}"></script>
<script src="{{asset('frontend/js/owl.carousel.min.js')}}"></script>
<script src="{{asset('frontend/js/jquery.magnific-popup.min.js')}}"></script>
<script src="{{asset('frontend/js/jquery.sticky.js')}}"></script>
<script src="{{asset('frontend/js/main.js')}}"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use App\Category;
use App\Http\Controllers\CommentController;
use App\Mail\NewPost;
use App\Post;
use App\Tag;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Route;

Expand Down Expand Up @@ -71,3 +73,17 @@
$recentPosts = Post::latest()->take(3)->get();
return $view->with('categories', $categories)->with('recentPosts', $recentPosts)->with('recentTags', $recentTags);
});


// Send Mail
Route::get('/send', function(){
$post = Post::findOrFail(7);
// Send Mail
Mail::to('user@user.com')
->bcc(['user1@user.com','user2@user.com'])
->queue(new NewPost($post));



return (new App\Mail\NewPost($post))->render();
});

0 comments on commit da47bf5

Please sign in to comment.