简体   繁体   English

Codeigniter 无法将 email 发送到存储在数据库中的地址

[英]Codeigniter cannot sent email to address stored in database

I already try when using static email address it's worked.在使用 static email 地址时,我已经尝试过了。 But, when i want to sent email who stored in database it's not working.但是,当我想发送存储在数据库中的 email 时,它不起作用。

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.当我再次尝试解决这个问题时,我发现我的问题实际上是 preg_match() 期望参数 2 是字符串,给定数组。

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遇到 PHP 错误

Severity: Warning严重性:警告

Message: preg_match() expects parameter 2 to be string, array given消息:preg_match() 期望参数 2 是字符串,给定数组

Filename: libraries/Email.php文件名:libraries/Email.php

Line Number: 1070行号:1070

Backtrace:回溯:

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

File: C:\xampp\htdocs\projek\application\controllers\Email.php Line: 128 Function: sentmail文件:C:\xampp\htdocs\projek\application\controllers\Email.php 行:128 Function:已发送邮件

File: C:\xampp\htdocs\projek\index.php Line: 315 Function: require_once文件:C:\xampp\htdocs\projek\index.php 行: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设置为生成的结果集第一行中email列的字符串值,可以使用以下命令:

$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.但是对于只从单行返回一个值的方法,方法名称应该是getLastEmailAddress()并且它应该返回一个字符串。 The method end with:该方法以:

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

and the controller declaration should look like: controller 声明应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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