So I've been tasked with creating a function for our member database that will alert someone on a monthly basis as to when their membership is going to expire. We send out one email if a person is 6 months or less from their membership expiration, and another if they are 3 months out. No big whoop. I first create a model like this:
class Email extends CI_Model {
function __construct()
{
parent::__construct();
}
// -----------------EMAIL-----------------
function emailMessage($message = 1,$recipient = 1777) { // Default "Test" Numbers
$rInfo = $this->db->query("SELECT * FROM Members WHERE intKey = " . $recipient)->row();
$eInfo = $this->db->query("SELECT * FROM Emails WHERE intKey = " . $message)->row();
// Replacements?
$bodyCopy = $eInfo->txtEmail;
$bodyCopy = str_replace("[firstname]",$rInfo->strFirstName,$bodyCopy);
$this->load->library('email');
$this->email->from('someone@example.com','John Q Public');
$this->email->to($rInfo->strEmail);
$this->email->subject($eInfo->strTitle);
$this->email->message($bodyCopy);
$this->email->send();
$this->email->clear();
}
}
All fine and good, and when I call this model from any other part of the site I have built, it works fine. It's a great way to handle any stock emails that have to be sent out to our constituency. Normally, it's just something like this within the code:
$this->load->model('email');
$this->email->emailMessage(8,$this->session->userdata('memberKey'));
Works like a charm. The big difference between where I've used it previously, and where I use it now is that I'm enclosing the call to the model within a loop as follows:
public function warningEmails() {
$this->load->model('email');
$sql = 'SELECT
*, FLOOR(
DATEDIFF(dtAccreditationEnd, now())/ 30
)AS diff
FROM
tblMembers
WHERE
enmAccredited = "yes"
AND dtAccreditationEnd < DATE_ADD(now(), INTERVAL 6 MONTH)
ORDER BY
diff
';
$emailSend = $this->db->query($sql);
foreach ($emailSend->result() as $row) {
if ($row->diff <= 3) {
$letter = 10;
} else {
$letter = 9;
}
$this->email->emailMessage($letter,$row->intMembersKey);
}
}
It works great the first time through the loop, and fails with the following message the very next iteration through the loop:
Fatal error: Call to undefined method CI_Email::emailMessage() in /home/contract/public_html/members/application/controllers/staff.php on line 1033
Line 1033 is this line:
$this->email->emailMessage($letter,$row->intMembersKey);
I there a problem where this model can only be called once in a given instance? What am I doing wrong? Thank you in advance for your assistance!
EDIT: I'm going to add a var_dump($this->email); to see what it could be...
object(CI_Email)#30 (46) { ["useragent"]=> string(11) "CodeIgniter" ["mailpath"]=> string(18) "/usr/sbin/sendmail"
["protocol"]=> string(4) "mail" ["smtp_host"]=> string(0) ""
["smtp_user"]=> string(0) "" ["smtp_pass"]=> string(0) ""
["smtp_port"]=> string(2) "25" ["smtp_timeout"]=> int(5)
["smtp_crypto"]=> string(0) "" ["wordwrap"]=> bool(true)
["wrapchars"]=> string(2) "76" ["mailtype"]=> string(4) "text"
["charset"]=> string(5) "utf-8" ["multipart"]=> string(5) "mixed" ["alt_message"]=> string(0) "" ["validate"]=>
bool(false) ["priority"]=> string(1) "3" ["newline"]=>
string(1) " " ["crlf"]=> string(1) " " ["send_multipart"]=>
bool(true) ["bcc_batch_mode"]=> bool(false) ["bcc_batch_size"]=> int(200) ["_safe_mode"]=> bool(false) ["_subject"]=> string(0) "" ["_body"]=> string(357) "Geoffrey,This is a reminder that your PDCA Accreditation will expire in 3 months. Visit your Membership Dashboard to view all complete and incomplete course work at your convenience. The Membership Dashboard will assist you through the completion process. For more information, please contact Someone at Someone@example.com or 1-800-555-1212." ["_finalbody"]=> string(0) ""
["_alt_boundary"]=> string(0) "" ["_atc_boundary"]=> string(0) "" ["_header_str"]=> string(0) "" ["_smtp_connect"]=>
string(0) "" ["_encoding"]=> string(4) "8bit" ["_IP"]=>
bool(false) ["_smtp_auth"]=> bool(false) ["_replyto_flag"]=>
bool(false) ["_debug_msg"]=> array(0) { } ["_recipients"]=>
string(17) "recipient@example.com" ["_cc_array"]=> array(0) { } ["_bcc_array"]=> array(0) { } ["_headers"]=> array(5) { ["From"]=> string(54) ""Someone Example" " ["Return-Path"]=> string(28) "" ["Cc"]=> string(16) "copy@example.com" ["Bcc"]=> string(17) "blindcopy@example.com" ["Subject"]=> string(49) "=?utf-8?Q?PDCA_Accreditation_Expiration_Warning?=" } ["_attach_name"]=> array(0) { } ["_attach_type"]=> array(0) { } ["_attach_disp"]=> array(0) { } ["_protocols"]=> array(3) { [0]=> string(4) "mail" [1]=> string(8) "sendmail" [2]=> string(4) "smtp" } ["_base_charsets"]=> array(2) { [0]=> string(8) "us-ascii" [1]=> string(9) "iso-2022-" } ["_bit_depths"]=> array(2) { [0]=> string(4) "7bit" [1]=> string(4) "8bit" } ["_priorities"]=> array(5) { [0]=> string(11) "1 (Highest)" [1]=> string(8) "2 (High)" [2]=> string(10) "3 (Normal)" [3]=> string(7) "4 (Low)" [4]=> string(10) "5 (Lowest)" } }