How can I send an email to many recipients in laravel?

Viewed 123

I can send emails to a lot of recipients but the problem is I am using a business email which I'm subscribe to my web host, the problem is when I send 1 email to a lot of recipients my web host automatically suspends my email's outgoing which technically I'm having problems with right now, so I tried using mailing list but it won't send emails to my recipients using the Mail(), but it works when sending a mail to mailist-join@domain.com.

Controller:

public function imail($request){

    $dataEmail = [
        'date' => $request->date,
        'time_start' => $request->start,
        'time_end' => $request->end,
        'duration' => abs(strtotime($request->end) - strtotime($request->start))/(60*60),
        'areas' => Purifier::clean($request->areas),
        'reason' => Purifier::clean($request->activities)
    ];
    $emails = UserEmail::where('email','!=','')->select('email')->get()->pluck('email');
    $subject = 'ADVISORY (' . date("F j, Y",strtotime($request->date)) .')';

    foreach ($emails as $email) {
        Mail::to($email)
        ->send(new SendToAll($dataEmail,$subject));
    }
}

SendToAll Mail

    <?php

namespace App\Mail;

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

class SendToAll extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;
    // public $afterCommit = true;
    public $data,$subject;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data,$subject)
    {
        $this->data = $data;
        $this->subject = $subject;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject($this->subject)
        ->from('myemail@domain.com','Me')
        ->view('pages.imail')
        ->with('data',$this->data);
        ;
    }
}

I also tried supervisor but to no avail. I'm using Windows Server 2012.

1 Answers

You have to use like below code

Mail::to($email)->cc(['mail1','mail2','mail3'])->send(new SendToAll($dataEmail,$subject));
Related