CodeIgniter中从数据库获取发件人邮箱替代硬编码的实现方法问询
Got it, let's get that hardcoded sender email replaced with a dynamic value from your database. Here's exactly how to do it in your CodeIgniter model and controller:
Step 1: Update Your Model to Fetch Email from Database
First, add a helper method in your model to retrieve the greeting email from the common_email_settings table. Then modify your existing email function to use this dynamic value instead of the hardcoded string.
Modified Model Code
function send_user_email_missing_doc_test($to, $subject, $body, $attachment = null,$cc = NULL, $bcc = NULL,$bcc2 = NULL) { $this->load->library('email'); $this->email->clear(TRUE); $config['mailtype'] = 'html'; $this->email->initialize($config); // Fetch greeting email from database instead of hardcoding $greeting_email = $this->get_greeting_email(); $this->email->from($greeting_email, 'Sharestates'); $this->email->reply_to('Allen@investorsloanservicing.com'); if ($cc != '') { $this->email->cc('help@ssapp.support'); } if ($bcc != '') { $ary=array('help@ssapp.support,Allen@investorsloanservicing.com'); $this->email->bcc($ary); } if ($bcc2 != '') { $ary=array('help@ssapp.support'); $this->email->bcc($ary); } $this->email->to($to); $this->email->subject($subject); $this->email->message($body); if (isset($attachment) && !empty($attachment)) { foreach ($attachment as $path) { $this->email->attach($path); } } $this->email->send(); } // Helper method to retrieve the greeting email from DB private function get_greeting_email() { // Query the table for the record where name = 'greeting_email' $query = $this->db->get_where('common_email_settings', ['name' => 'greeting_email']); // Return the email value if record exists if ($query->num_rows() > 0) { return $query->row()->value; } // Fallback to original hardcoded value as a safety net return 'Greetings@investorsloanservicing.com'; }
Step 2: Controller Usage
Your controller doesn’t need major changes—just load the model as usual and call the email function like you did before. Here’s a quick example:
// In your controller $this->load->model('Your_Model_Name'); // Replace with your actual model name // Call the email sending function $this->Your_Model_Name->send_user_email_missing_doc_test( 'user@example.com', 'Missing Documents Notification', '<p>Please submit the required documents...</p>' // Add attachment, cc, bcc parameters if needed );
Optional: Optimize with Caching
To avoid querying the database every time you send an email (which can add unnecessary overhead), you can cache the email value in the model. This way, it’s fetched once per request:
// Add a private property to cache the email private $greeting_email = null; private function get_greeting_email() { // Return cached value if available if ($this->greeting_email !== null) { return $this->greeting_email; } $query = $this->db->get_where('common_email_settings', ['name' => 'greeting_email']); if ($query->num_rows() > 0) { $this->greeting_email = $query->row()->value; } else { $this->greeting_email = 'Greetings@investorsloanservicing.com'; } return $this->greeting_email; }
内容的提问来源于stack exchange,提问作者Nino C Philip

