1

I have tried send email to multiple recipients. I input the recipients into an array, like this:

$recipients = array(
            '[email protected]',
            '[email protected]',
            '[email protected]'
        );

And it works nice.

Now I need to get the email address form database, and when I run it, it shows error:

A PHP Error was encountered

Severity: Warning

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

Filename: libraries/Email.php

Line Number: 795

I tried to see the array from the database, and it looks like this:

Array ( [0] => Array ( [email] => [email protected] ) [1] => Array ( [email] => [email protected] ) )

Here are the function to send email:

function send_bulk_mail() {
        $from = '[email protected]';
        $to = '[email protected]';
        $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'));
    }

Is there any solution to change the array? Or for change the rules in libraries/email.php

This is the function in 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;
    }

Thank you...

2
  • Can you paste the code where it's breaking? libraries/Email.php line 795 (the whole enclosing function) Commented Aug 13, 2014 at 1:45
  • Done, sir. Please check it. Thank you. Commented Aug 13, 2014 at 1:47

2 Answers 2

1

The $recipients you retrived from DB is an array with each element is an array, while the $recipients you described below is an array with each element is a string. Try to make an array with each element is a string when you retrive from DB like this :

$recipient_array = $this->company_model->get_email($search_by, $search_field);
$recipients = array();
foreach($recipient_array as $key => $value) {
     $recipients[] = $value['email'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried to call the array result using this code print_r($recipients); and the result still same with the above.
0

preg_match expects two strings and then an array. (http://php.net/manual/en/function.preg-match.php). The error seems to indicate that it's not getting a properly formatted array as the second parameter.

Based on the proper formatting of an array (http://php.net/manual/en/function.array.php) compared to your array output it seems as if you are giving it an array of arrays that enumerated with the 'email' index. (much like the 'fruits' example in the provided link).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.