0

Is anyone can suggest what is the best way or how can I send email to all the users that are located in my news_subscibers table with dynamic data from a form? I tried and was a able to send email to a hard coded email.

 public function sendNewsEmail(Request $request)
{
    $this->validate($request,[
      'subject' => 'bail|string|required|string|max:100',
      'bodymessage' => 'bail|string|required|string|min:10',
    ]);

    $data = array(
      'subject' => $request->subject,
      'bodymessage' => $request->bodymessage
    );


    $subscriber_emails = NewsSubscriber::pluck('subs_email')->toArray();

    foreach ($subscriber_emails as $mail)
    {
    Mail::send('email.news-email', $data, $mail,  function($message) use ($data, $mail){
          $message->from('[email protected]');
          $message->to('[email protected]');
          $message->cc($mail);
          $message->subject($data['subject']);
      });

      Session::flash('success', 'Your message was sent!');
      return redirect()->back();

    };


}

I would like to send the email to all users in news_subscribers table.

3 Answers 3

1

You can use mailable to send multiple emails

generating new mailable

php artisan make:mail SampleMail

this command will generate a file in app/mail/SampleMail.php optional write some logic in a mailable class

call mailable in controller using

$user = User::get(); // fetch user's to send mails
Mail::to($user)->send(new SampleMail()); 

mailable docs - https://laravel.com/docs/5.8/mail#generating-mailables

Sign up to request clarification or add additional context in comments.

Comments

0

you just pass your array in $message->to($subscriber_emails) no need foreach loop to send email to multiple user

Mail::send('email.news-email', $data, $mail,  function($message) use ($data, $subscriber_emails){
      $message->from('[email protected]');
      $message->to($subscriber_emails);
      $message->subject($data['subject']);
  });

3 Comments

I'm getting thie error: call_user_func() expects parameter 1 to be a valid callback, function '[email protected]' not found or invalid function name
DO NOT DO THIS. It will put all of the emails in the "to" list, and EVERY recipient will see all of the emails. You do not want to send all of the email addresses in your system to every person.
@Aken Roberts, I can use bcc to hide recipients emails. But Knuks suggestion worked fine. If you have better method to handle this kinda request, please share the code. Thanks.
0

You have your success flash message and returned response inside of your $subscriber_emails loop. This causes only the first email to send, then the loop stops and returns the redirect response.

Place those lines after to keep the loop going for all emails.

public function sendNewsEmail(Request $request)
{
    // ...


    $subscriber_emails = NewsSubscriber::pluck('subs_email')->toArray();

    foreach ($subscriber_emails as $mail)
    {
        Mail::send('email.news-email', $data, $mail, function ($message) use ($data, $mail) {
            $message->from('[email protected]');
            $message->to('[email protected]');
            $message->cc($mail);
            $message->subject($data['subject']);
        });
    }

    Session::flash('success', 'Your message was sent!');

    return redirect()->back();
}

(Assuming this is your issue. If not, you should add any error messages or unexpected behavior you're experiencing.)

1 Comment

I'm getting thie error: call_user_func() expects parameter 1 to be a valid callback, function '[email protected]' not found or invalid function name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.