You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter框架下从MySQL获取邮箱实现批量邮件发送

Got it, let's walk through exactly how to pull multiple recipient emails from your MySQL database and use them in CodeIgniter's email library with that comma-separated format you need. Here's a straightforward, clean implementation split between model and controller:

Step 1: Build the Model Method to Fetch Emails

First, create or update your model (let's say Email_model.php) to query the database and format the emails into a single comma-separated string. This keeps your database logic separated from the controller, which follows CodeIgniter's best practices.

<?php
class Email_model extends CI_Model {

    public function get_recipient_emails() {
        // Adjust the table and column names to match your database
        $this->db->select('email');
        $query = $this->db->get('recipients');

        if ($query->num_rows() > 0) {
            // Extract the email values into an array
            $emails = $query->result_array();
            // Convert the array to a comma-separated string
            return implode(',', array_column($emails, 'email'));
        }

        // Return empty string if no recipients found (avoids errors)
        return '';
    }
}
Step 2: Implement the Controller Logic

Next, in your controller (e.g., Email_controller.php), load the necessary libraries and model, fetch the formatted emails, and send the message. Don't forget to configure your email settings (either in config/email.php or directly in the controller).

<?php
class Email_controller extends CI_Controller {

    public function send_bulk_email() {
        // Load required libraries and model
        $this->load->library('email');
        $this->load->model('Email_model');

        // Fetch comma-separated recipients from the model
        $recipient_emails = $this->Email_model->get_recipient_emails();

        // Check if we have recipients before proceeding
        if (empty($recipient_emails)) {
            echo "No recipients found in the database.";
            return;
        }

        // Configure email settings (adjust these to match your setup)
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'your-smtp-host.com';
        $config['smtp_port'] = 587;
        $config['smtp_user'] = 'your-email@example.com';
        $config['smtp_pass'] = 'your-email-password';
        $config['mailtype'] = 'html'; // or 'text' for plain text
        $config['charset'] = 'utf-8';

        $this->email->initialize($config);

        // Set email details
        $this->email->from('your-sender-email@example.com', 'Your Name');
        $this->email->to($recipient_emails); // Here's your comma-separated string!
        $this->email->subject('Bulk Email Subject');
        $this->email->message('Hello, this is a bulk email sent to all recipients from the database.');

        // Send the email and handle results
        if ($this->email->send()) {
            echo "Bulk email sent successfully!";
        } else {
            // For debugging, you can view the error message
            echo "Email failed to send. Error: " . $this->email->print_debugger();
        }
    }
}
Quick Notes to Keep in Mind
  • Database Table Adjustment: Make sure the select('email') and get('recipients') match your actual database column and table names.
  • Email Validation: It’s a good idea to add a validation check for valid email formats in your model or database (using MySQL's VALIDATE EMAIL constraint or CodeIgniter's form validation if you're inserting emails).
  • Large Recipient Lists: If you have hundreds of recipients, some email providers might flag comma-separated to addresses as spam. In that case, consider using $this->email->bcc($recipient_emails) instead, or loop through each email individually (though that's slower).
  • Config Setup: You can also move the email configuration to application/config/email.php so you don’t have to initialize it in every controller.

内容的提问来源于stack exchange,提问作者Farz Ad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:13:45