Laravel发送包含多个产品信息的邮件

时间:2016-06-15 15:43:22

标签: php laravel email

我已经实现了多个产品上传并将详细信息保存到数据库,我需要将包含多个产品数据的邮件发送到邮件

我的控制器:

 public function store(Request $request)
   {
      $input = Input::all();
      $destinationPath = public_path()  . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'trade'; // upload path
      $files = Input::file('file');

      $length = 6;
      $randomString = substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);

    $trade = Trade::create(array( 
        'id' => $request->get('id'),           
        'first_name' => $request->get('first_name'),
        'last_name' => $request->get('last_name'),
        'address1' => $request->get('address1'),
        'address2' => $request->get('address2'),
        'zip_code' => $request->get('zip_code'),
        'country' => $request->get('country'),
        'email' => $request->get('email'),
        'phone' => $request->get('phone'),
        'bank_name' => $request->get('bank_name'),
        'account_number' => $request->get('account_number'),
        'iban_number' => $request->get('iban_number'),
        'swift_number' => $request->get('swift_number'),
        'voucher_code' => $randomString . $request->get('voucher_code'),
    ));

    foreach ($input['purchase_item'] as $k => $v) { 
        $file = $input['file'][$k];
        if (!file_exists($destinationPath)) {
            mkdir($destinationPath, 0777, true);
        } 
        $extension = $file->getClientOriginalExtension(); 
        $file_name = time() . '_' . $file->getClientOriginalName();   
        $fileName = $file_name; // renameing image
        $file->move($destinationPath, $fileName);                           
        $timestamp = strtotime($input['purchase_date'][$k]); 
        $new_date = date('Y-m-d', $timestamp);
        $tradeins_products  = TradeProduct::create(array(
            'tradins_id' => $trade->id,  
            'product_id' => $input['product_id'][$k],
            'application_id' => $input['application_id'][$k],
            'brand_id' => $input['brand_id'][$k],
            'purchase_date' => $new_date,
            'purchase_item' => $input['purchase_item'][$k]   ,
            'file' => $fileName               

         ));
       $data = array(
            'first_name' => $request->get('first_name'),
            'last_name' => $request->get('last_name'),
            'email' => $request->get('email'),
            'product_name' => $tradein_product_name[0]['name'],
            'address' => $request->get('address1'),
            'phone' => $request->get('phone'),
            'bank_name' => $request->get('bank_name'),
            'account_number' => $request->get('account_number'),
            'purchase_date'=>$new_date,
            'country_email'=>$country_name[0]['email'],
            'country_address1' =>$country_name[0]['address_1'],
            'country_address2'=>$country_name[0]['address_2'],
            'country_city'=>$country_name[0]['city'],
            'country_state'=>$country_name[0]['state'],
            'country_zipcode'=>$country_name[0]['zipcode'],
            'voucher_code'=>$trade->voucher_code               
        );
       Mail::send('frontend.mail_trade', $data, function($message) use ($data){
        $message->to($data['email'], $data['first_name'].' '.$data['last_name'])->subject('Product Trade-in');            
   return Response::json(array('voucher_code' => $trade- >voucher_code,'first_name'=>$trade->first_name,'email'=>$trade->email,'phone'=>$trade->phone,'address1'=>$trade->address1,'zip_code'=>$trade->zip_code,'country'=>$trade->country,'country_name' => $country_name[0]['name'],'voucher_code1' => $trade->voucher_code,'country_address1' =>$country_name[0]['address_1'],'country_address2'=>$country_name[0]['address_2'],'country_city' =>$country_name[0]['city'],'country_state' =>$country_name[0]['state'],'country_zipcode' =>$country_name[0]['zipcode'], 'country_email' =>$country_name[0]['email'], 'company_name' => $country_name[0]['company_name']));

}

所以上面我插入了两个表,tradin和tradein产品,所以tradein产品包含多个产品上传和插入,所以我需要在邮件上显示如下

产品数据: 1)产品名称      - 索尼     -三星 2)申请名称:     -Handsfree     -无线 那么如何使用foreach或任何其他替代方法来显示邮件模板中的数据呢

我的邮件模板:

<h3>Cash Back</h3>
<p>Product Name:{{$product_name}} </p>
<p>Name: {{$first_name}} {{$last_name}}
</p>
<p>Address: {{$address}}</p>
<p>Phone:{{$phone}} </p>
<p>Bank: {{$bank_name}}</p>
<p>Account Number:{{$account_number}} </p>
<p>Voucher Code:{{$voucher_code}} </p>
<p>Company Email:{{$country_email}} </p>
<p>Company Address1:{{$country_address1}} </p>
<p>Company Address2:{{$country_address2}} </p>
<p>Company City:{{$country_city}} </p>
<p>Company State:{{$country_state}} </p>
<p>Company Zipcode:{{$country_zipcode}} </p>
<p>Purchase Date: {{$purchase_date}}</p>

1 个答案:

答案 0 :(得分:0)

请注意,第一个参数是您的电子邮件视图,第二个参数是您要传递给视图的数据。例如:

Mail::send('frontend.mail_trade', $productsData, function($message) use ($data){
   // your code
});