简体   繁体   中英

Send email format html in codeigniter

i want to send email from codeigniter by getting recipient from database and message in html format with some data from database like name. This is my controller code.

function send_email(){
    $config = array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'shin@gmail.com', 
        'smtp_pass' => '2341',
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
    );

    $message = '
    <html>
        <head>
            <title>Packet</title>
        </head>
        <body>
        <p>you have book this</p>
        <table>
            <tr>
                <td>ID Booking</td>
                <td>'.$id_booking.'</td>
            </tr>
            <tr>
                <td>Price</td>
                <td>'.$price'</td>
            </tr>
            <tr>
                <td>Name</td>
                <td>'.$name.'</td>
            </tr>

        </table>
        </body>
    </html>
    ';

    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->set_mailtype("html");
    $this->email->from('shin@gmail.com', 'Shin');
    $this->email->to($user);
    $this->email->subject($subject);
    $this->email->message($message);    

    if($this->email->send())
    {
        echo 'Email sent.';
    }
    else
    {
        show_error($this->email->print_debugger());
    }
}

my database consist booking table with id_booking, price, name, email. So the problem is i dont know how to get user email from database and get $id_booking, $price and $name ? sorry i'm new at learning programming..

here is simple way.step by step.
First open your database.php at application/config/ folder and fill this

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'your database hostname';
$db['default']['username'] = 'your database username';
$db['default']['password'] = 'your database password';
$db['default']['database'] = 'your database name';
$db['default']['dbdriver'] = 'mysql';//or your dbdriver

you can set other fields if you need at this file.

second step: create a file mymodel.php inside your model folder. and write these codes

<?php
class Mymodel extends CI_Model 
{

 public function __construct()
 {
    parent::__construct();
    $this->load->database();
 }
 public function getUser()
 {
    $this->db->from("your table name");
    $this->db->select("email,id_booking,price,name ");
    $users=$this->db->get()->result();
    return $users;

 }

}  

You can create your own model with different name
third call this model function inside your controller function

function send_email()
{
    $this->load->model("mymodel");
    $users=$this->mymodel->getUser();
    //now you will get all users here as object.
    //your rest of the code here
}

hope this will help you.

这是一个使用CodeIgniter电子邮件库发送电子邮件的示例,该库http://4rapiddev.com/php/codeigniter-send-email-via-smtp-charset-utf-8/在该示例中,您将需要配置自己的SMTP。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM