我想使用Laravel 4发送电子邮件。从mysql中检索电子邮件和用户名列表,如下所示:
[{"user_id":1,"first_name":"foo","last_name":"bar","email":"foobar@email.com"}, ..
我能够从mysql检索名字,姓氏和电子邮件,但我如何将其传递给Mail:send功能并使用Gmail发送电子邮件?邮件配置已设置为默认设置,并且某些邮件使用不同的发件人名称和电子邮件发送。
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('name@email.com', 'name')
->from('different_email@gmail.com', 'Another name')
->subject('Laravel Email Test 1');
});
您只需要传入值并循环遍历要发送到
的用户列表$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('different_email@gmail.com', 'Another name')
->subject('Laravel Email Test 1');
});
}
您可以将地址数组传递给您的->to
:
Mail::send('mail_template1', array('name' => 'Laravel'), function($message) use ($arr_index)
{
$message->to(array(
'name@email.com',
'anothername@email.com' => 'name'
))
->from('different_email@gmail.com', 'Another name')
->subject('Laravel Email Test 1');
});