2016-12-14 90 views
0

我正在嘗試創建一個按鈕,該按鈕使用複選框類型輸入向任何電子郵件發送邀請。我真的需要你的幫助,如何做到這一點。
這是我的看法代碼:如何使用複選框將電子郵件發送給多個收件人?

<form role="form" action="{{Extras::getEUserURLPath(Auth::user()->user_id, '/ajax/sendFriendsInvitation')}}" method="GET"> 
    <div class="row form-group" style="padding-left: 50px;"> 
     <?php $i=0 ?> 
     @if($friends) 
     @foreach($friends as $friend) 
     <?php $member = \App\User::find($friend); $i++;?> 
     @if (Auth::user()->user_id != $member->user_id) 
     <input type="checkbox" id="cb{{$i}}" value="{{ $member->user_id }}" name="mem[]"> 
     <label class="col-md-5 boxed" for="cb{{$i}}"> 
      {{isset($member->email) ? $member->email: ''}} 
     </label> 
     @endif 
     @endforeach 
     @endif 
    </div> 
    <div class="row form-inline" align="center" style="margin-left: -20%;"> 
     <button type="submit" value="Submit" class="btn btn-sm btn-success invitation" title="Send Invitation"><i class="fa fa-plus"></i> Add Friends</button> 
    </div> 
</form> 

,我想我現在只需要將sendFriendsInvitation以及

public function sendFriendsInvitation(Request $request, $account, $group){ 
    $group = \App\Group::where('alias', 'LIKE', $group)->get(); 
    $muser = Extras::getUserInfo($group[0]->user_id); 

    $arde = \App\Email::where('type', '=', 'uinv')->get(); 
    $content = ""; 
    $subject = ""; 
    if ($arde->count() > 0) { 
     $content = $arde[0]->content; 
     $subject = str_replace(array('$resort_name', '$event_name'), array($resortName, $groupName),$arde[0]->subject); 
    } else { 
     $subject = 'Welcome to ' . $resortName . '! |' . $groupName; 
     $content = view('emails.content.content-of-friends-registration'); 
    } 

    $access_info = '<p>Just follow the link below to accept your invitation. </p>'; 
    $access_info .= '<p>Click <a href="#">here</a> and use your email ('.$muser->email.') to register.</p>'; 

    $keywords = [ 
    'firstname' => $muser->first_name, 
    'resort_name' => $resortName, 
    'group_name' => $groupName, 
    'access_info' => $access_info 
    ]; 
    $ar_key = array(); 
    $ar_val = array(); 
    foreach ($keywords as $key => $value) { 
     $ar_key[] = "$".$key; 
     $ar_val[] = $value; 
    } 
    $contation = str_replace($ar_key, $ar_val, $content); 

    $mdata = array(
     'event_group_name' => $event_group->name, 
     'firstname' => $muser->first_name, 
     'lastname' => $muser->last_name, 
     'email' => $muser->email, 
     'code' => $muser->activation_code, 
     'utype' => "member", 
     'resort_name' => $resortName, 
     'group_name' => $groupName, 
     'created_by' => $request->input('first_name') . " " . $request->input('last_name'), 
     'company_email' => $owner->email, 
     'company_phone' => $owner->phone, 
     'company_website' => "", 
     'logo_img' => Extras::getMeta('logo', '\App\GroupMeta', $group[0]->group_id), 
     'header_img' => asset('storage/images/banner/' . $banner), 
     'social_fb' => Extras::getMeta('social_fb', '\App\GroupMeta', $group[0]->group_id), 
     'social_tw' => Extras::getMeta('social_tw', '\App\GroupMeta', $group[0]->group_id), 
     'social_insta' => Extras::getMeta('social_insta', '\App\GroupMeta',$group[0]->group_id), 
     'content' => $contation 
     ); 

    $m_email = $muser->email; 
    $m_fname = $muser->first_name; 
    $m_lname = $muser->last_name; 

    Mail::send('emails.content.friends-registration', $mdata, function ($message) use ($owner, $m_email, $m_fname, $m_lname, $resortName, $groupName) { 

     $message->from($owner->email, $resortName)->to($m_email, $m_fname . ' ' . $m_lname)->subject('Welcome to ' . $resortName . '! |' . $groupName); 

    }); 
    return response()->json(['response' => true]); 
} 
+0

的郵件「到」方法接受值的數組。因此,通過點擊查看輸入的ID,您可以獲取他們的電子郵件並將其放入數組中。 例如:$ m_email = ['[email protected]','ja[email protected]']; –

+0

謝謝你的回覆。我應該如何把它放到$ mdata?我很抱歉,但這不是我的代碼。我只是用它作爲基礎。這是我現在使用的,但我不知道如何製作一個。 –

+0

哦,我認爲'user_id'將用於'sendFriendsInvitation' for'$ muser = Extras :: getUserInfo($ group [0] - > user_id);' –

回答

0

如果你想使用複選框僅用於發送電子郵件,然後您可以將輸入元素更改爲:

<input type="checkbox" id="cb{{$i}}" value="{{ $member->email }}" name="mem[]"> 

從'user_id'到'email'。

那麼php方法「sendFriendsInvitation」可以是:

public function sendFriendsInvitation(Request $request, $account, $group){ 

$mails = []; 

foreach($request->mem as $key => $val){ 
    $mails[] = $val; 
} 


Mail::send('emails.content.friends-registration', $mdata, function ($message) use ($owner, $m_email, $m_fname, $m_lname, $resortName, $groupName) { 

     $message->from($owner->email, $resortName)->to($mails, $m_fname . ' ' . $m_lname)->subject('Welcome to ' . $resortName . '! |' . $groupName); 

    }); 


} 
+1

你可以保留其餘的代碼,加: $ mails = []; foreach($ request-> mem as $ key => $ val){ $ mails [] = $ val; } –

相關問題