php - Codeigniter cannot sent email to address stored in database
Get the solution ↓↓↓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' => '[email protected] ',
'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 [email protected]
2 Philipp [email protected]
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
Answer
Solution:
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 theemail
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 begetLastEmailAddress()
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();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 7.3.0".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.