2014-11-06 51 views
0

我想從codeigniter發送電子郵件,通過從數據庫和消息以html格式從數據庫中獲取某些數據(如名稱)來獲取收件人。這是我的控制器代碼。在codeigniter中發送電子郵件格式html

function send_email(){ 
    $config = array(
     'protocol' => 'smtp', 
     'smtp_host' => 'ssl://smtp.googlemail.com', 
     'smtp_port' => 465, 
     'smtp_user' => '[email protected]', 
     '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('[email protected]', '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()); 
    } 
} 

我的數據庫包含id_booking,價格,名稱,電子郵件的預訂表。 所以問題是我不知道如何從數據庫中獲取用戶電子郵件,並獲得$ id_booking,$ price和$ name?對不起,我是學習編程的新手..

+2

我希望這不是你真正的用戶名和密碼。 – 2014-11-06 18:40:34

+0

通常您會在模型中創建函數來獲取行,然後您可以從控制器調用該函數。 – 2014-11-06 18:41:47

回答

-1

以下是使用CodeIgniter電子郵件庫發送電子郵件的示例,http://4rapiddev.com/php/codeigniter-send-email-via-smtp-charset-utf-8/ 您將需要在示例中配置自己的SMTP。

+0

他似乎知道如何發送電子郵件。他在問如何從數據庫中獲取值。此外,這裏不鼓勵使用僅鏈接的答案。嘗試將鏈接中的代碼添加到您的答案中,以防連接斷開。 – 2014-11-06 18:42:58

1

這裏是簡單的方法。一步一步。
首先打開你在申請/配置/文件夾database.php文件,並填寫此

$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 

,如果你需要在這個文件中,您可以設置其他領域。

第二步:在您的模型文件夾內創建一個文件mymodel.php。寫這些代碼

<?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; 

} 

} 

你可以用不同的名稱創建自己的模型
第三呼叫控制器功能裏面這種模式功能

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 
} 

希望這會幫助你。