AKmahato
8 months ago

How to fetch data from database and send through the automated mail

Posted 8 months ago by AKmahato

I have created an automated mail that is scheduled, and the mail contains the count of users, but we want to show the content of table( id, name) which will go as mail to the email id provided.

Here is my code


public function handle()
    {
        $data1 = DB::table('users')->select('id','created_at')->get();
       
        foreach($data1 as $data){
            $fromData = $data->created_at;
          
                $date = Carbon\Carbon::now();
                $difference = $date->diffInDays($fromData);
                if($difference>=7){
                    $totalUsers = \DB::table('users')
                          ->whereRaw('Date(created_at) = CURDATE()')
                          ->count();
                        Mail::to('[email protected]')
                        //->subject($subject)
                        ->cc('[email protected]')
                        ->send(new SendMailable($totalUsers));
        
                }
         }

This is my SendMailable class


public $count;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.registeredcount')
        ->subject('This is Demo Email');
    }

This is my view

<div>
    Total number of registered users for today is: {{ $count }}
</div>

Please sign in or create an account to participate in this conversation.