• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending Emails from Database

#1
[eluser]RMinor[/eluser]
In my application I will need to occasionally send emails based on certain criteria from a database. What I want to do is store the sent emails in an array and the failed emails in an array and then display them on the view when the script is done running. What I have so far is below, but I am not sure if this will work for me. Can anyone see anything I am doing wrong or could be doing better?

Code:
public function sendApproveEmail($email)
{
    $result = array();
    $body = $this->_retrieveEmailBody('approved');
    $this->email->from($this->_from_address, $this->_from_name);
    $this->email->to($email);  
    $this->email->subject('');
    $this->email->message($body);
    if ($this->email->send()) {
        $result = array(
            'sent' => array($email)
        );
    } else {
        $result = array(
            'failed' => array($email)
        );
    }
    return $result;
}

Then in the view would I access this array by:
Code:
foreach ($result as $email) {
    foreach ($sent as $successful) {
        // Do something
    }
    foreach ($failed as $failure) {
        // Do something
    }
}

#2
[eluser]TWP Marketing[/eluser]
[quote author="RMinor" date="1346772260"]

Code:
...
    if ($this->email->send()) {
        $result = array(
            'sent' => array($email)
        );
    } else {
        $result = array(
            'failed' => array($email)
        );
    }
...
[/quote]
I think you need to index the 'sent' and 'failed' arrays or you will overwrite them and only find one of each instance in $result

#3
[eluser]RMinor[/eluser]
How would I index those arrays? Sorry, I am a bit confused on how to do that with multidimensional arrays.

#4
[eluser]TWP Marketing[/eluser]
This is one way:
it makes each array entry unique, preventing overwriting
Code:
...
    $s = 0; // index for 'sent' array
    $f = 0; // index for 'failed' array
    if ($this->email->send()) {
        $result = array(
            'sent' => array($s => $email)
        );
        $s++; // increment the index for the next use
    } else {
        $result = array(
            'failed' => array($f => $email)
        );
        $f++; // increment the index for the next use
    }
...

#5
[eluser]RMinor[/eluser]
Thanks so much. I think I was guilty of over-thinking the solution.

#6
[eluser]TWP Marketing[/eluser]
Let me know if that works for you

#7
[eluser]RMinor[/eluser]
[quote author="TWP Marketing" date="1346777655"]Let me know if that works for you[/quote]

Okay, I will be able to test it later this evening when I get home. Thanks again!

#8
[eluser]RMinor[/eluser]
Now that I think about how I have my code set up I am not sure that it will work. Take a look below and see what I mean.

Controller (important parts of it)
Code:
// Retrieve all persons that need to be sent emails
$persons = $this->Person_model->getPersonsByCasting($id);
foreach ($persons as $recipient) {
    // Make sure the person has not received an email already
    if ($recipient['person_email_sent'] == 'No') {
        // Determine which email to send to each person
        switch ($recipient['person_status']) {
            // Send approved email
            case 'approved':
                $data['emails'] = $this->Email_model->sendApprovedEmail($recipient['person_email']);
                break;
            // Send deleted email
            case 'deleted':
                $data['emails'] = $this->Email_model->sendDeletedEmail($recipient['person_email']);
                break;
            // Send on file email
            case 'file':
                $data['emails'] = $this->Email_model->sendFileEmail($recipient['person_email']);
                break;
            // Send hold email
            case 'hold':
                // Generate a random password for the person to log in with
                $password = $this->Person_model->holdPerson($recipient['person_id']);
                $data['emails'] = $this->Email_model->sendHoldEmail($recipient['person_email'], $password);
                break;
        }
    }
    // Load the required view
    $this->load->view('submission-emails_view', $data);

Model (each method looks the same)
Code:
/**
* Method to send email to deleted persons
* @param string $email
*/
public function sendDeletedEmail($email)
{
    $result = array();
    $body = $this->_retrieveEmailBody('deleted');
    $this->email->from($this->_from_address, $this->_from_name);
    $this->email->to($email);  
    $this->email->subject($this->_retrieveEmailBody('deleted'));
    $this->email->message($body);
    $s = 0;
    $f = 0;
    if ($this->email->send()) {
        $result = array(
            'sent' => array($s => $email)
        );
        $s++;
    } else {
        $result = array(
            'failed' => array($f => $email)
        );
        $f++;
    }
    return $result;
}

Then in my view I wanted to display a list of all sent and all failed emails. I am not sure if my current setup will accomplish this.

#9
[eluser]TWP Marketing[/eluser]
Code:
// Retrieve all persons that need to be sent emails
$persons = $this->Person_model->getPersonsByCasting($id);
foreach ($persons as $recipient) {
    // Make sure the person has not received an email already
    if ($recipient['person_email_sent'] == 'No') {
        $id = $recipient['person_email_sent']; // get the unique id of recipient
        // Determine which email to send to each person
        switch ($recipient['person_status']) {
            // Send approved email
            case 'approved':
                $data['emails'][$id] = $this->Email_model->sendApprovedEmail($recipient['person_email']);
                break;
            // Send deleted email
            case 'deleted':
                $data['emails'][$id] = $this->Email_model->sendDeletedEmail($recipient['person_email']);
                break;
            // Send on file email
            case 'file':
                $data['emails'][$id] = $this->Email_model->sendFileEmail($recipient['person_email']);
                break;
            // Send hold email
            case 'hold':
                // Generate a random password for the person to log in with
                $password = $this->Person_model->holdPerson($recipient['person_id']);
                $data['emails'][$id] = $this->Email_model->sendHoldEmail($recipient['person_email'], $password);
                break;
        }
    }
    // Load the required view
    $this->load->view('submission-emails_view', $data);
I think you need to apply the same concept to the data array. a unique key value for each email.
I assumed you have a var 'id' in each $persons record. I used that to add the unique key to $data['emails'][$id']. I assume that your model will read that id value and pass it back in the $persons array...
IF YOU DON'T, then you need to find some other unique identifier for each recipient. Let me know if this works.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.