How to fetch record from database and send it to email using codeigniter?

Member

by samara , in category: PHP Frameworks , 10 months ago

How to fetch record from database and send it to email using codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 10 months ago

@samara 

To fetch a record from the database and send it to an email using CodeIgniter, you can follow the steps below:


Step 1: Configure SMTP in CodeIgniter


First, configure the SMTP settings in your CodeIgniter project. Open the "config.php" file located in the "application/config" folder and update the following SMTP settings:

1
2
3
4
5
6
7
8
9
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'your_smtp_host';
$config['smtp_port'] = 'your_smtp_port';
$config['smtp_user'] = 'your_smtp_username';
$config['smtp_pass'] = 'your_smtp_password';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'html';
$config['newline'] = "
";


Replace "your_smtp_host", "your_smtp_port", "your_smtp_username", and "your_smtp_password" with your SMTP server details.


Step 2: Create a Model


Create a model to fetch the record from the database. Open the "Email_model.php" file located in the "application/models" folder and add the following code:

1
2
3
4
5
6
7
class Email_model extends CI_Model {

    public function getRecord() {
        $query = $this->db->get('your_table_name');
        return $query->result();
    }
}


Replace "your_table_name" with the name of the table from which you want to fetch the record.


Step 3: Create a Controller


Create a controller named "Email" to send the record to an email. Open the "Email.php" file located in the "application/controllers" folder and add the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Email extends CI_Controller {

    public function index() {
        $this->load->library('email');
        $this->load->model('email_model');
        
        $record = $this->email_model->getRecord();
        $data['record'] = $record;
        
        $this->email->from('[email protected]', 'Your Name');
        $this->email->to('[email protected]');
        $this->email->subject('Database Record');
        $this->email->message($this->load->view('email_template', $data, true));
        
        if ($this->email->send()) {
            echo 'Email sent successfully.';
        } else {
            echo 'Email sending failed.';
        }
    }
}


Replace "[email protected]", "Your Name", and "[email protected]" with the appropriate email addresses.


Step 4: Create a View


Create a view named "email_template.php" to format the email content. In the "application/views" folder, create a file named "email_template.php" and add the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<html>
<head>
    <title>Database Record</title>
</head>
<body>
    <h1>Database Record</h1>
    <?php foreach ($record as $row) : ?>
        <p>Name: <?php echo $row->name; ?></p>
        <p>Email: <?php echo $row->email; ?></p>
        <p>Phone: <?php echo $row->phone; ?></p>
    <?php endforeach; ?>
</body>
</html>


Customize the email template according to your needs.


Step 5: Run the Code


Finally, open a web browser and visit the following URL to trigger the email sending process:

1
http://localhost/your_project_name/index.php/email


Replace "your_project_name" with the name of your CodeIgniter project. If everything is set up correctly, the record fetched from the database will be sent to the specified email address.