7

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.

Ali Özen
  • 1,525
  • 2
  • 14
  • 29
  • you need to be more specific abt all types of errors. – Parth Pandya Mar 29 '18 at 09:36
  • 1
    If you get all sorts of errors a good place to start is https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – apokryfos Mar 29 '18 at 09:44
  • Please check my below answer. – Pratik Mehta Mar 29 '18 at 10:26
  • If you don't want all recipients of your email to see all other email addresses, you should consider using `Mail::bcc($emails)->send(...)` instead of `Mail::to($emails)->send(...)`. And if you have the requirement that each recipient gets an individual link in the email (like a registration link), you'll have to send one mail per address anyway - meaning none of the above solutions will work. – Namoshek Mar 29 '18 at 11:09
  • When i said all king of errors. I mean i worked a lot. But there is no code here right now. This is the last thing i made. Errors are about mailing system. I just don't know how to send bulk mail. – Ali Özen Mar 29 '18 at 14:20

3 Answers3

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

    };

}
tringuyen
  • 791
  • 5
  • 5
2

$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.

Ali Özen
  • 1,525
  • 2
  • 14
  • 29
1

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,

Pratik Mehta
  • 707
  • 8
  • 27