Cryssie
Cryssie

Reputation: 3175

How to pass emails info stored in array to Mail::send using Gmail in Laravel 4.2

I want to use Laravel 4 to send emails. The list of emails and user names are retrieved from mysql like below:

[{"user_id":1,"first_name":"foo","last_name":"bar","email":"[email protected]"}, ..

I am able to retrieve the first name, last name and email from mysql but how do I pass it to Mail:send function and use Gmail to send out the emails? The mail config has been set to a default settings and some emails are sent out using a different sender name and email.

app/config/mail.php

return array(

    'driver' => 'smtp',

    'host' => 'smtp.gmail.com',

    'port' => 465, 

    'from' => array('address' => 'my_gmail_username.gmail.com', 'name' => 'Test Email'),

    'encryption' => 'ssl',

    'username' => 'my_gmail_username',

    'password' => 'my_gmail_password',

    'sendmail' => '/usr/sbin/sendmail -bs',

    'pretend' => false,

app/routes.php

Mail::send('mail_template1', array('name' => 'Laravel'), function($message) use ($arr_index)
            {
                $message->to('[email protected]', 'name')
                        ->from('[email protected]', 'Another name')
                        ->subject('Laravel Email Test 1');   

            });

Upvotes: 0

Views: 2027

Answers (2)

JofryHS
JofryHS

Reputation: 5874

You can pass array of addresses to your ->to:

Mail::send('mail_template1', array('name' => 'Laravel'), function($message) use ($arr_index)
{
    $message->to(array( 
                    '[email protected]', 
                    '[email protected]' => 'name'
                ))
            ->from('[email protected]', 'Another name')
            ->subject('Laravel Email Test 1');   

});

Upvotes: 1

Brobin
Brobin

Reputation: 3326

You need to just pass the values in and loop through your list of users to send to

$users = //Get your array of users from your database

foreach($users as $user)
{
    Mail::send('mail_template1', array('name' => 'Laravel'), function($message) use ($user)
    {
        $message->to($user->email, $user->first_name.' '.$user->last_name)
                ->from('[email protected]', 'Another name')
                ->subject('Laravel Email Test 1');   

    });
}

Upvotes: 2

Related Questions