Sunday, 6 December 2020

How to send email in Laravel 7 - 8

Step 1. Create mail
             php artisan make:mail Contact Mail

<?php
namespace App\Mail;

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

class ContactMail extends Mailable
{
    use Queueable, SerializesModels;
    public $details;

    public function __construct($details
    {
       $this->details = $details;    
    }

  
  public function build()    
    {
        return $this->view('email');    
    }

Step 2: In the controller file, get the details of the fiels.   
use Illuminate\Support\Facades\Mail;

            $name = $request->input('name');
            $contact_email = $request->input('contact_email');   
            $subject = $request->input('subject');   
            $message = $request->input('message');               
            $receiver = 'rameshyadavjee@gmail.com';
           
            $details = [       
                'name' =>  $name,       
                'contact_email'=> $contact_email,
                'subject' => $subject,
                'message' => $message,
                'receiver' => $receiver
            ];
           
            Mail::to($receiver)->send(new \App\Mail\ContactMail($details));      
        
  // To use below one  use App\Mail\NewRegistrationMail;
           //Mail::to($receiver)->send(new NewRegistrationMail($details));         
         

Step 3: Create Blade View
             resources/views/email.blade.php

<html>
<head>
<title>New Inquiry</title>
</head>
<body>
<strong>Subject</strong>&nbsp;:&nbsp;{{$_REQUEST['subject']}}<br>
<strong>Name</strong>&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;{{$_REQUEST['name']}}<br>
<strong>Email</strong>&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;{{$_REQUEST['contact_email']}}<br>
<strong>Message</strong>&nbsp;:&nbsp;{{$_REQUEST['message']}}
</html>

or

<strong>Subject</strong>&nbsp;:&nbsp;{{$details['subject']}}<br>
<strong>Name</strong>&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;{{$details['name']}}<br>
<strong>Email</strong>&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;{{$details['contact_email']}}<br>
<strong>Message</strong>&nbsp;:&nbsp;{{$details['message']}}


That's it

No comments:

Post a Comment

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...