【问题标题】:using array data from database for send email to multiple recipients codeigniter使用数据库中的数组数据向多个收件人发送电子邮件
【发布时间】:2014-08-13 01:42:40
【问题描述】:

我已尝试向多个收件人发送电子邮件。我将收件人输入到一个数组中,如下所示:

$recipients = array(
            'email1@host.com',
            'email2@host.com',
            'email3@host.com'
        );

而且效果很好。

现在我需要获取电子邮件地址表单数据库,当我运行它时,它显示错误:

遇到了 PHP 错误

严重性:警告

消息:preg_match() 期望参数 2 是字符串,给定数组

文件名:libraries/Email.php

行号:795

我试图从数据库中查看数组,它看起来像这样:

Array ( [0] => Array ( [email] => vikki2@afteroffice.com ) [1] => Array ( [email] => nanapuspita05@hotmail.com ) )

这里是发送电子邮件的功能:

function send_bulk_mail() {
        $from = 'email@email.com';
        $to = 'email1@email.com';
        $search_by = $this->input->post('search_by');
        $search_field = $this->input->post('search_field');
        $recipients = $this->company_model->get_email($search_by, $search_field);;
        $subject = $this->input->post('subject');
        $message = $this->input->post('message');

        $this->load->library('email');
        $this->email->from($from);
        $this->email->to($to);
        $this->email->bcc($recipients);
        $this->email->subject($subject);
        $this->email->message($message);
        $this->email->send();

        redirect(base_url('index.php/company'));
    }

有没有办法改变数组?或者更改libraries/email.php中的规则

这是libraries/email.php中的函数:

public function clean_email($email)
    {
        if ( ! is_array($email))
        {
            if (preg_match('/\<(.*)\>/', $email, $match))
            {
                return $match['1'];
            }
            else
            {
                return $email;
            }
        }

        $clean_email = array();

        foreach ($email as $addy)
        {
            if (preg_match( '/\<(.*)\>/', $addy, $match))
            {
                $clean_email[] = $match['1'];
            }
            else
            {
                $clean_email[] = $addy;
            }
        }

        return $clean_email;
    }

谢谢...

【问题讨论】:

  • 你能把代码粘贴到它出错的地方吗? library/Email.php 第 795 行(整个封闭函数)
  • 完成,先生。请检查一下。谢谢。

标签: php arrays codeigniter email


【解决方案1】:

您从 DB 中检索的 $recipients 是一个数组,其中每个元素都是一个 array,而您在下面描述的 $recipients 是一个每个元素的数组都是一个字符串。当您像这样从数据库中检索时,尝试使每个元素都是一个字符串的数组:

$recipient_array = $this->company_model->get_email($search_by, $search_field);
$recipients = array();
foreach($recipient_array as $key => $value) {
     $recipients[] = $value['email'];
}

【讨论】:

  • 我已尝试使用此代码print_r($recipients); 调用数组结果,结果仍与上述相同。
【解决方案2】:

preg_match 需要两个字符串,然后是一个数组。 (http://php.net/manual/en/function.preg-match.php)。该错误似乎表明它没有将格式正确的数组作为第二个参数。

基于与您的数组输出相比的数组 (http://php.net/manual/en/function.array.php) 的正确格式,您似乎给它提供了一个使用“电子邮件”索引枚举的数组数组。 (很像提供的链接中的“水果”示例)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2021-07-18
    • 2012-05-18
    • 2016-05-22
    • 1970-01-01
    相关资源
    最近更新 更多