php - 向数据库记录发送电子邮件时出现 Codeigniter 错误

标签 php mysql database codeigniter email

我正在尝试从输入表单向我的数据库列表中的多个联系人发送电子邮件,当我硬编码一个特定的电子邮件地址时它有效,但是当我尝试引用我的数据库时,我收到错误:

消息:mail():SMTP 服务器响应:503 5.0.0 需要 RCPT(收件人) 文件名:libraries/Email.php

我还收到另一个错误,我认为这可能只是我一般的电子邮件配置问题,但我不确定;

无法使用 PHP mail() 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。

我的 Controller :

public function sendmail() {
$this->load->library('email');
$this->load->model('Email_model');
$this->load->library('session');

$this->email->from($this->input->post('email'), $this->input->post('name'));

$sendTo['email'] = $this->Email_model->emailsend();

$this->email->to($sendTo);
$this->email->subject('Hello This is an email');

$this->email->message($this->input->post('message'));



if ($this->email->send()){
$this->session->set_flashdata('success','Email has been sent');
redirect('dashboard');
}else{


echo $this->email->print_debugger();

}

我的模型

class Email_model extends CI_Model {


public function emailsend() {

  $query = $this->db->query("SELECT email from contacts");
  $sendTo=array();
  foreach ($query->result() as $row) 
  {
      $sendTo['email']=$row->email; 
  }

我的观点

<form action="<?php echo site_url('/Dashboard/sendmail');?>" method="post" accept-charset="utf-8">

<?php echo form_open('/Dashboard/sendmail');?>

  <label for="name">Your Name</label><input id="name" type="text" name="name">

  <label for="email">Your Email</label><input id="email" type="text" name="email">

  <label for="message">Your Message</label>
  <textarea id="message" name="message" rows="8" cols="50"></textarea>

  <input id-"submit" type="submit" name="submit" value="submit">
<?php echo form_close(); ?>

<p><?php echo $flash;?></p>

对任何一个 errpr 的任何帮助将不胜感激

最佳答案

CodeIgniter 的电子邮件模块需要 an array of email addressesto() 函数中。您的模型的 emailsend() 函数向该数组添加了一个 email 索引,这不是预期的。此外,您将在 foreach 循环的每次迭代中覆盖 email 索引的值,这意味着您的数组将只包含数据库中的最后一个地址。

看起来您的函数在填充后实际上并未返回数组,但这部分可能只是在您的问题中被截断了。

public function emailsend() {

    $query = $this->db->query("SELECT email from contacts");
    $sendTo=array();
    foreach ($query->result() as $row) 
    {
        $sendTo[] = $row->email;
        //     ^^ remove the 'email' index
    }

    return $sendTo; // <-- add this line if you don't have it
}

然后,从您的 Controller 中,您不需要将结果存储在单独的变量中,除非您想对其执行一些额外的逻辑。你可以简单地这样做:

$this->email->from($this->input->post('email'), $this->input->post('name'));
$this->email->to($this->Email_model->emailsend());
$this->email->subject('Hello This is an email');

关于php - 向数据库记录发送电子邮件时出现 Codeigniter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49285106/

相关文章:

mysql - 如何使用内连接优化mysql查询

mysql - 选择注册同一类(class)但不认识任何人的人

php - html5 验证在 iPhone 上不起作用

php - 使用类时查询速度慢

PHP 函数调用放置

mysql - SQL:从另一个数据集添加多列

mysql - LEFT JOIN 之后的 INNER JOIN

php - mysql_result 资源无效

javascript - 将 MySQL "where"转换为 Sequelizejs "where"

mysql - 如何使用两个分类条件过滤wordpress选择查询?