使用foreach和数组向多个收件人发送电子邮件

时间:2016-12-15 15:41:59

标签: php arrays email

我正在尝试向多个收件人发送电子邮件,但事实证明该邮件仅发送给所选的最后一个用户。你们可以检查一下我的代码并告诉我,我做错了什么吗? 这是代码:

public function sendFriendsInvitation(Request $request, $account, $group){
    $account = \App\Account::where( 'alias', 'LIKE', $account )->get();
    $group = \App\Group::where( 'alias', 'LIKE', $group )->get();
    $event_group = EventGroup::find( $request->event_group_id);
    $resortName = $group[0]->trip_name;
    $groupName = $group[0]->name;
    $owner = User::find($group[0]->user_id);
    $banner = Extras::getMeta( 'header_email', '\App\GroupMeta', $group[0]->group_id );
    $musers = array();
    foreach ($request->user_id as $user_id) {
        $musers[] = User::find($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');
    }

    foreach($musers as $muser){
        $access_info = '<p>Just follow the link below to register or join. </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(
                       'title' => 'Welcome to ' . $resortName,
                       'event_group_name' => $event_group->name,
                       'firstname' => $muser->first_name,
                       'lastname' => $muser->last_name,
                       'email' => $muser->email,
                       '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 个答案:

没有答案