Send Email Via Sendgrid Laravel Project Example Code

Spread the love

Project:- Send Email Via Sendgrid Laravel Project Example Code

Sign Up for Sendgrid

Send Email Via Sendgrid Laravel Project Example Code

1. First of all edit Your .env file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=send grid username
MAIL_PASSWORD=sendgrid password
MAIL_ENCRYPTION=tls

2. Run Artisan Command

php artisan make:mail TestMail

3. Edit TestMail from App\Mail\TestMail.php

<?php

namespace App\Mail;

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

class TestEmail extends Mailable
{
    use Queueable, SerializesModels;
   
    public $data;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $address = '[email protected]';
        $subject = "This is a Demo!!!!";
        $name = "Enter the Your Company Name";
        return $this->view('emails.test')->from($address, $name)->subject($subject)->with(['data' => $this->data]);
    }
}

4. Now create test.blade.php in resources\views\emails\test.blade.php 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Test Data</title>
</head>
<body>
	<h2>Test Email Hindi Alerts Demo!!!!</h2>
	<p>{{$data['message']}}</p>
</body>
</html>

5. Make Form in welcome.blade.php

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
        <link rel="stylesheet" href="{{asset('css/app.css')}}">
    </head>
    <body>
        @if(session('error'))
            <div class="alert alert-danger">
                {{session('error')}}
            </div>
        @endif
        @if(session('success'))
            <div class="alert alert-success">
                {{session('success')}}
            </div>
        @endif
        <div class="container">
            <div class="row">
                <div class="col-md-4 offset-4">
                    <form action="test" method="POST">
                        {{ csrf_field() }}
                        <div class="form-group">
                            <label for="emails">Enter Some Text</label>
                            <textarea class="form-control" type="text" name="message"></textarea>
                        </div>
                        <div class="form-group">
                            <label>Email</label> 
                            <input class="form-control" type="email" name="email"> 
                        </div>
                        <input type="submit" name="toEmail" class="btn btn-warning" value="Send Email">
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

6. Now Edit web.php from routes\web.php

<?php

Route::get('/', function () {
    return view('welcome');
});

Route::any ('test', function () {
    if (Request::get('message' ) != null)
        $data = ['message' => Request::get('message')];
    else{
        return redirect('/')->with('error', "Please Enter your message");
    }
    if (Request::get('email' ) != null){
        Mail::to(Request::get('email'))->send(new TestEmail($data));
    	return redirect('/')->with('success', "Email Sent Succesfully");
    }
    else{
    	return redirect('/')->with('error', "Please Enter your Email ID");
    }
});

7. Now You Try to send message….

Hire For New Project

Tag:- sendgrid smtp api, sendgrid smtp settings, sendgrid api, sendgrid send email Laravel, sendgrid smtp api key, sendgrid pricing, email sending service, sendgrid documentation, Send Email with PHP and SendGrid, Send Email with Laravel & SendGrid, How to Send an SMTP Email – SendGrid Documentation,

Be the first to comment

Leave a Reply

Your email address will not be published.


*