1 year ago

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

Posted 1 year 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);
                    $totalUsers = \DB::table('users')
                          ->whereRaw('Date(created_at) = CURDATE()')
                        Mail::to('[email protected]')
                        ->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

    Total number of registered users for today is: {{ $count }}

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