Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 sending mail to multiple users at once

I am trying to sending mails to all users. But i can't figure out how to make this. In my controller, i made this.

public function send_mail()
{
    $mails = Joinus::all();
    $array = array();
    $allmails = array();
    foreach ($mails as $mail)
    {
        $allmails = array_push($array, $mail->email);

    };
    Mail::to($allmails)->send((new SendMail(new Joinus('email')))->delay(30));
}

I am getting all types of error. Last one is

__construct() must be of the type array

In my SendMail.php

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

I wasted my one day and can't make. I am very grateful for your help. Thanks an advance.

like image 705
Ali Özen Avatar asked Mar 29 '18 09:03

Ali Özen


People also ask

How do I send multiple emails in smtp?

For smtp you need to have port number, logon email address and in To:"[email protected];[email protected]" …

How many ways we can send mail in Laravel?

Sending an email in Laravel Laravel's creators recommend using one of the API based drivers: Mailgun, SparkPost, or Amazon SES.


3 Answers

public function send_mail()
{
    $mails = Joinus::pluck('email')->toArray();
    foreach ($mails as $mail)
    {
        Mail::to($mail)->send((new SendMail(new Joinus($mail)))->delay(30));

    };

}
like image 184
tringuyen Avatar answered Sep 19 '22 13:09

tringuyen


$allmails = array_push($array, $mail->email); is wrong

Right answer is just array_push($array, $mail->email);

array_push($array, $mail->email); this is returning an array.

$allmails = array_push($array, $mail->email); But this is returning int value.

like image 38
Ali Özen Avatar answered Sep 20 '22 13:09

Ali Özen


You can try this.

public function send_mail()
{
    $mails = Joinus::all();
    $array = array();
    $allmails = array();
    foreach ($mails as $mail)
    {
        $allmails = array_push($array, $mail->email);

    };
    Mail::to($allmails)->send(new SendMail(new Joinus('email')))->delay(30);
}

Thanks,

like image 44
Pratik Mehta Avatar answered Sep 23 '22 13:09

Pratik Mehta