src/SendEmails/SendEmailsFromList.php
public function sendEmails($listID, $data) {

        // if the list is not enabled, then return false
        if (!$this->helpers->isListEnabled($listID)) {
            return false;
        }

        $emailData = [];

        // subject
        $emailData['subject'] = $this->helpers->getListNameFromListID($listID) . ' - ' . $data['title'];

        // build the individual emails
        $emailIDs = $this->helpers->getEnabledEmailsFromList($listID);

        if (count($emailIDs) == 0) {
            // for some reason, maybe mismatched list_id #'s, finding no emails
            return false;
        }

        // link to the post
        $data['link']  = '<a href="';
        $data['link'] .= $data['canonical_url'];
        $data['link'] .= '">';
        $data['link'] .= $data['title'];
        $data['link'] .= '</a>';

        // iterate through each email address, prepping and sending each email individually!
        foreach ($emailIDs as $emailID) {

            // if emails must be type "primary" AND the email is *not* type primary, do not process
            if (!$this->helpers->isEmailAddressPrimaryType($emailID->email_id)) {
                continue;
            }

            ///////////////////////////////////////////////////////////////////////////////////////////
            ////                         Unsubscribe from List                                     ////
            ///////////////////////////////////////////////////////////////////////////////////////////

            // Create a token
            $unsubscribeToken = $this->createUnsubscribeToken->createUniqueToken();

            // INSERT to the list_unsubscribe_token database table
            $this->createUnsubscribeToken->createTokenRecord($listID, $emailID->email_id, $unsubscribeToken);

            // Build the unsubscribe link
            $data['unsubscribe_link'] = $this->createUnsubscribeToken->unsubscribeURL($unsubscribeToken);

            ///////////////////////////////////////////////////////////////////////////////////////////
            ////                     Send email to a list recipient                                ////
            ///////////////////////////////////////////////////////////////////////////////////////////

            // get the actual email address from the "email_id" field in the "list_email" db table
            $emailData['to_email'] = $this->helpers->getEmailAddressFromEmailID($emailID->email_id);

            // get the person's first name and surname
            $emailData['to_name'] = $this->helpers->getFirstnameSurnameFromEmailID($emailID->email_id);

            $data['to_email'] = $emailData['to_email'] ;
            $data['to_name']  = $emailData['to_name'];

            // send the email one-at-a-time
            // note the queue-ing
            Mail::queue('lasallecrmlistmanagement::emails.send_email_from_list', ['data' => $data], function($message) use ($emailData)
            {
                $message->from(Config::get('lasallecmscontact.from_email'), Config::get('lasallecmscontact.from_name'));
                $message->to($emailData['to_email'], $emailData['to_name']);
                $message->subject($emailData['subject']);
            });
        }

        return true;
    }
public function sendEmails($listID, $data) {

        // if the list is not enabled, then return false
        if (!$this->helpers->isListEnabled($listID)) {
            return false;
        }

        $emailData = [];

        // subject
        $emailData['subject'] = $this->helpers->getListNameFromListID($listID) . ' - ' . $data['title'];

        // build the individual emails
        $emailIDs = $this->helpers->getEnabledEmailsFromList($listID);

        if (count($emailIDs) == 0) {
            // for some reason, maybe mismatched list_id #'s, finding no emails
            return false;
        }

        // link to the post
        $data['link']  = '<a href="';
        $data['link'] .= $data['canonical_url'];
        $data['link'] .= '">';
        $data['link'] .= $data['title'];
        $data['link'] .= '</a>';

        // iterate through each email address, prepping and sending each email individually!
        foreach ($emailIDs as $emailID) {

            // if emails must be type "primary" AND the email is *not* type primary, do not process
            if (!$this->helpers->isEmailAddressPrimaryType($emailID->email_id)) {
                continue;
            }

            ///////////////////////////////////////////////////////////////////////////////////////////
            ////                         Unsubscribe from List                                     ////
            ///////////////////////////////////////////////////////////////////////////////////////////

            // Create a token
            $unsubscribeToken = $this->createUnsubscribeToken->createUniqueToken();

            // INSERT to the list_unsubscribe_token database table
            $this->createUnsubscribeToken->createTokenRecord($listID, $emailID->email_id, $unsubscribeToken);

            // Build the unsubscribe link
            $data['unsubscribe_link'] = $this->createUnsubscribeToken->unsubscribeURL($unsubscribeToken);

            ///////////////////////////////////////////////////////////////////////////////////////////
            ////                     Send email to a list recipient                                ////
            ///////////////////////////////////////////////////////////////////////////////////////////

            // get the actual email address from the "email_id" field in the "list_email" db table
            $emailData['to_email'] = $this->helpers->getEmailAddressFromEmailID($emailID->email_id);

            // get the person's first name and surname
            $emailData['to_name'] = $this->helpers->getFirstnameSurnameFromEmailID($emailID->email_id);

            $data['to_email'] = $emailData['to_email'] ;
            $data['to_name']  = $emailData['to_name'];

            // send the email one-at-a-time
            // note the queue-ing
            Mail::queue('lasallecrmlistmanagement::emails.send_email_from_list', ['data' => $data], function($message) use ($emailData)
            {
                $message->from(Config::get('lasallecmscontact.from_email'), Config::get('lasallecmscontact.from_name'));
                $message->to($emailData['to_email'], $emailData['to_name']);
                $message->subject($emailData['subject']);
            });
        }

        return true;
    }
public function sendEmails ( $ listID , $ data ) { // if the list is not enabled, then return false if ( ! $ this -> helpers -> isListEnabled ( $ listID ) ) { return false ; } $ emailData = [ ] ; // subject $ emailData [ 'subject' ] = $ this -> helpers -> getListNameFromListID ( $ listID ) . ' - ' . $ data [ 'title' ] ; // build the individual emails $ emailIDs = $ this -> helpers -> getEnabledEmailsFromList ( $ listID ) ; if ( count ( $ emailIDs ) == 0 ) { // for some reason, maybe mismatched list_id #'s, finding no emails return false ; } // link to the post $ data [ 'link' ] = '<a href="' ; $ data [ 'link' ] .= $ data [ 'canonical_url' ] ; $ data [ 'link' ] .= '">' ; $ data [ 'link' ] .= $ data [ 'title' ] ; $ data [ 'link' ] .= '</a>' ; // iterate through each email address, prepping and sending each email individually! foreach ( $ emailIDs as $ emailID ) { // if emails must be type "primary" AND the email is *not* type primary, do not process if ( ! $ this -> helpers -> isEmailAddressPrimaryType ( $ emailID -> email_id ) ) { continue ; } /////////////////////////////////////////////////////////////////////////////////////////// ////                         Unsubscribe from List                                     //// /////////////////////////////////////////////////////////////////////////////////////////// // Create a token $ unsubscribeToken = $ this -> createUnsubscribeToken -> createUniqueToken ( ) ; // INSERT to the list_unsubscribe_token database table $ this -> createUnsubscribeToken -> createTokenRecord ( $ listID , $ emailID -> email_id , $ unsubscribeToken ) ; // Build the unsubscribe link $ data [ 'unsubscribe_link' ] = $ this -> createUnsubscribeToken -> unsubscribeURL ( $ unsubscribeToken ) ; /////////////////////////////////////////////////////////////////////////////////////////// ////                     Send email to a list recipient                                //// /////////////////////////////////////////////////////////////////////////////////////////// // get the actual email address from the "email_id" field in the "list_email" db table $ emailData [ 'to_email' ] = $ this -> helpers -> getEmailAddressFromEmailID ( $ emailID -> email_id ) ; // get the person's first name and surname $ emailData [ 'to_name' ] = $ this -> helpers -> getFirstnameSurnameFromEmailID ( $ emailID -> email_id ) ; $ data [ 'to_email' ] = $ emailData [ 'to_email' ] ; $ data [ 'to_name' ] = $ emailData [ 'to_name' ] ; // send the email one-at-a-time // note the queue-ing Mail :: queue ( 'lasallecrmlistmanagement::emails.send_email_from_list' , [ 'data' => $ data ] , function ( $ message ) use ( $ emailData ) { $ message -> from ( Config :: get ( 'lasallecmscontact.from_email' ) , Config :: get ( 'lasallecmscontact.from_name' ) ) ; $ message -> to ( $ emailData [ 'to_email' ] , $ emailData [ 'to_name' ] ) ; $ message -> subject ( $ emailData [ 'subject' ] ) ; } ) ; } return true ; }
Send emails from a list.

Expected that this method called from another package, via an event job

@param  int     $listID    Database table "lists" ID
@param  array   $data      Post's title, body, etc
@return bool
Send emails from a list .