Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Build a newletter

Hi All,

I need to build a small app/page that will select all users from the database then within a text area add some text and email all the users at the same time, how is the best way to do this?

0 likes
29 replies
theUnforgiven's avatar

I'm thinking something like MailChimp but I already have over 100 registered users so will that still work?

theUnforgiven's avatar

Yes I'm using Mandrill already to send out individual emails but how would I send out a group email to the 100 + email address at once.

theUnforgiven's avatar

So in this method,

Mail::send('emails/order', $data, function( $message ) use ($data)
        {
            $message->to('')
                ->from('test@email.com')
                ->subject('Newsletter');
        });

How would I say to get all email address and pass to the to() call ?

theUnforgiven's avatar

Ok get that, but I want to jut call from my controller something like User::all(); pass that the mail function like you stated but how can that be done?

theUnforgiven's avatar

ok appreciate your help, but with that in mind how about passing that to:

$data = [
            'content' => Input::get('content'),
            'recipients' => array(

            )
        ];
theUnforgiven's avatar

Would this

$user = User::all()->lists('email');


        $data = [
            'content' => Input::get('content'),
            'recipients' => $user
        ];

Work? Then passing $message->to($data['recipients']) like so?

ZetecVan's avatar

Wouldn't this have all the recipients in the 'to' field?

ie: if you were sending the mail to me, I would be able to see everyone else who you sent the email to. That may be an issue with privacy.

Or doesn't that matter?

theUnforgiven's avatar

Yes, that does matter actually, shit that's another spanner in the works then....

tkjaergaard's avatar
Level 5

I'm not sure where i should pass it to? What is your structure?

class SomeController extends Controller {

    public function sendEmails($message)
    {
        $recipients = User::all()->lists('email');

        Mail::send('email/newsletter', ['message'
 => $message], function($message) use ($recipients) {
             $message
             ->to($recipients)
             ->from('someEmail@example.com')
             ->subject('Newsletter');
        });

        return Redirect::back()->withMessage('Emails Sent');
    }

}

This could be a simple approach? Else you have to throw me some code to look at..

1 like
theUnforgiven's avatar

Basically I have a textarea that allows the admin to add some text etc then they will click send and that needs to pass that throw to the view as $data then send this to everyone in the Users table.

tkjaergaard's avatar

@lstables @ZetecVan Yeah, that might be the case, i'm not sure? Don't you have to put recipient and multiple CC recipients to be able to se who else it has been send to?

Else you're forced to do a simple foreach loop - Sending to one at the time?

theUnforgiven's avatar

sending one at a time wouldn't be all that bad how would that work then? I could use queue with mandrill also to make the process better.

tkjaergaard's avatar

Okay @lstables @ZetecVan - after a swift Google search, it seems that it sends them out one at the time unless you specify it to don't do it - so it should work? I would do a quick test to see if that's the case?

theUnforgiven's avatar

Oh so how you stated, it will send out one at a time then and disclose other users emails ?

theUnforgiven's avatar

Ok that works, but does show all emails in the to field in my email client. So obviously I don't want this, what do I need to in order to stop that? A loop? If so how?

tkjaergaard's avatar

Damn, okay.. That's a bugger :(

Okay the next simple approach i can think of is to do a simple foreach loop..

This could result in some "hanging" so you might want to introduce a queue here like AWS, Redis or Beanstalkd ...

theUnforgiven's avatar

I know what a bugger! based on the code you posted previoudly how would I use mail queue and loop?

tkjaergaard's avatar

The structure?

You should be able to do the first approach that I've suggested when you disable the option in Mandrill?

Please or to participate in this conversation.