eddy1992's avatar

How to fire a mail ?

Hi I want to fire a mail in this foreach and I am not sure how will I use this class to fire a mail. Now I have this class and it looks like this

<?php namespace App\Acme\Email;

use Illuminate\Support\Facades\Mail;
use App\Acme\Email\Contracts\EmailTransactions as EmailTransactionsContract;

/**
 * Flow Chart 
 *
 * 
 * we need :
 * customer email 
 * customer name 
 * view name
 * subject
 * data for view 
 * 
 * 
 */

/**
 * ClassName : EmailTransactions 
 *
 * @author  : Gogi <gogi@greenhonchos.in>
 */
class EmailTransactions implements EmailTransactionsContract
{

    protected $name;
    protected $email;
    protected $viewName;
    protected $subject;
    protected $dataForView;

    public function __construct($name, $email, $viewName, $subject, $dataForView)
    {
        $this->setName($name);
        $this->setEmail($email);
        $this->setViewName($viewName);
        $this->setSubject($subject);
        $this->setDataForView($dataForView);
    }

    public function sendEmail()
    {
        $subject = $this->getSubject();

        $response = Mail::queue($this->getViewName(), $this->getDataForView(), function ($message) {
      
              $message->to($this->getEmail(), $this->getName())
                      ->subject($this->getSubject());
      
        });

        return $response;
    }

    /**
    * Get name
    * @return  
    */
    public function getName()
    {
        return $this->name;
    }
    
    /**
    * Set name
    * @return $this
    */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
    * Get email
    * @return  
    */
    public function getEmail()
    {
        return $this->email;
    }
    
    /**
    * Set email
    * @return $this
    */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    /**
    * Get viewName
    * @return  
    */
    public function getViewName()
    {
        return $this->viewName;
    }
    
    /**
    * Set viewName
    * @return $this
    */
    public function setViewName($viewName)
    {
        $this->viewName = $viewName;
        return $this;
    }


    /**
    * Get subject
    * @return  
    */
    public function getSubject()
    {
        return $this->subject;
    }
    
    /**
    * Set subject
    * @return $this
    */
    public function setSubject($subject)
    {
        $this->subject = $subject;
        return $this;
    }

    /**
    * Get dataForView
    * @return  
    */
    public function getDataForView()
    {
        return $this->dataForView;
    }
    
    /**
    * Set dataForView
    * @return $this
    */
    public function setDataForView($dataForView)
    {
        $this->dataForView = $dataForView;
        return $this;
    }
}

Now I want to to fire this email in this foreach I would be able to do it but I am not sure how will use this class methods.

<?php

namespace App\Http\Controllers;
use App\Acme\Email\EmailTransactions; // I have imported this class 
use Illuminate\Http\Request;
use Laracasts\Flash\Flash;
use App\Http\Requests;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Admin\Traits\BulkUpload;
use App\Customer;
use App\Address;
class CustomerDataImport extends Controller
{
    use BulkUpload;

    public function getImport()
    {
        return view('backend.customerImport');
    }

    public function postStore(Request $request)
    {
        $input = $request->all();
          $file = $input['customer_file'];
        
          if ($this->saveFileToStorage($file, 'customer')) {    
                $results = Excel::load($this->getFilePathCustomer($file), function ($render) {
            })->all();
          if ($results->count()>20000) {
                    Flash::error('File exceeds the maximum rows limit');
                    return Redirect::to(route('customer/import'));
            }
            if ($results->count() == 0) {
                Flash::error('Uploaded file is empty. That is it contains no data');
                return Redirect::to(route('customer/import'));
          }
          return $this->startImport($results);    
      }
      else{
            Flash::error('Unable to upload file to storage .. please try again !!');
            return Redirect::to(route('customer.import'));
      }    
    }

  protected function startImport($results)
  {   
      foreach($results as $key => $result){
          //if email exist 
          if(Customer::where('email', $result->get('email'))->pluck('email')){

              Address::create([
                                  'customer_id'     => Customer::where('email', $result->get('email'))->pluck('id'),
                                  'name'            => $result->get('firstname').' '.$result->get('lastname'),
                                  'pincode'         => $result->get('pin'),
                                  'address'         => str_replace(",_x000D_",",",$result->get('postaladdress')),
                                  'city'            => $result->get('city'),
                                  'area'            => $result->get('otherarea'),
                                  'state'           => $result->get('state'),
                                  'phone'           => $result->get('mobileno'),
                                  'country'         => $result->get('country')
                              ]);
          }
          else{
              //if email doesnt exis then create new customer and update the address.
              Customer::create([
                                  'name'           => $result->get('firstname').' '.$result->get('lastname'),
                                  'email'          => $result->get('email'),
                                  'gender'         => $result->get('gender'),
                                  'phone'          => $result->get('mobileno'),
                                  'password'       => str_random(10),
                                  'account_status' => '1',
                              ]);
              

              Address::create([
                                  'customer_id'     => Customer::where('email', $result->get('email'))->pluck('id'),
                                  'name'            => $result->get('firstname').' '.$result->get('lastname'),
                                  'pincode'         => $result->get('pin'),
                                  'address'         => str_replace(",_x000D_",",",$result->get('postaladdress')),
                                  'city'            => $result->get('city'),
                                  'area'            => $result->get('otherarea'),
                                  'state'           => $result->get('state'),
                                  'phone'           => $result->get('mobileno'),
                                  'country'         => $result->get('country')
                              ]);

          }
          //I want to fire this mail in this foreach what would be the synatx 
      }
      echo "It has been imported. Cheers !";
  }

}

Please assist me with the syntax how will use that class and pass those parameters in that constructor

0 likes
0 replies

Please or to participate in this conversation.