简体   繁体   中英

Codeigniter cannot sent email to address stored in database

I already try when using static email address it's worked. But, when i want to sent email who stored in database it's not working.

here is my code:

//email.php
function sentmail($value='')
{
  $message = $this->load->view('templates/email',$value,TRUE);
  $config = array(
      'protocol' => 'smtp',
      'smtp_host' => 'mail.mail.co.id',
      'smtp_port' => 26,
      'smtp_user' => 'mail@mail.com ',
      'smtp_pass' => 'password',
      'mailtype' => 'html',
      'crlf' => "\r\n",
      'newline' => "\r\n"
    );
    $this->email->initialize($config);
    $this->email->set_newline("\r\n");
    $this->email->from($config['smtp_user']);
    $this->email->to($email);
    $this->email->subject('Email');
    $this->email->message($message);
    if($this->email->send())
  {
    echo "Email sudah terkirim.";
  }
 else
 {
     echo "Email gagal terkirim.";
     $this->email->print_debugger();
   }
}

//email_model.php
 function get_email()
  {
    $this->db->select('email');
    $this->db->from('tb_email');
    $this->db->order_by('id_email','DESC');
    $this->db->limit(1);
    $query = $this->db->get();
    return $query->result();
  }

and here is my table look like

tb_email
  id_email nama       email       
  1        Adam       x@mail.com  
  2        Philipp    y@mail.com 

FINALLY FOUND THE PROBLEM

When i trying again to fix this problem, i found that my problem is actually preg_match() expects parameter 2 to be string, array given.

Here is my code:

\\email.php
function process($id_email)//proses input email
{
    $email = $this->email_model->get_email();
    $where = array('id_email' => $id_email);
    $data['query_added'] = $this->email_model->latest_email($where,'tb_email');
    //var_dump($data);
    var_dump($email,$data);
    $this->sentmail($data);
    //$this->session->set_flashdata('input_success','<div class="alert alert-success" role="alert">Input sudah dibuat.</div>');
    //redirect('email');
}

And the error result is:

A PHP Error was encountered

Severity: Warning

Message: preg_match() expects parameter 2 to be string, array given

Filename: libraries/Email.php

Line Number: 1070

Backtrace:

File: C:\xampp\htdocs\projek\application\controllers\Email.php Line: 150 Function: to

File: C:\xampp\htdocs\projek\application\controllers\Email.php Line: 128 Function: sentmail

File: C:\xampp\htdocs\projek\index.php Line: 315 Function: require_once

There is much that I don't understand/like about these methods, but it is clear enough to me that:

$email = $this->email_model->get_email();

is returning an array of objects.

To set $email as the string value of the email column in the first row of your generated result set, you can use the following:

$email = $this->email_model->get_email()[0]->email;

But for a method that only returns one value from a single row, the method name should be getLastEmailAddress() and it should return a string. The method end with:

return $this->db->get()->row()->email;

and the controller declaration should look like:

$email = $this->email_model->getLastEmailAddress();

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