preg_match() expects parameter 2 to be string, object given error in CakePHP

22,190

EDIT: Completely missed the obvious problem that others have picked up...

So, I was looking for other issues that might have upset the class's configuration...

As per http://book.cakephp.org/2.0/en/core-utility-libraries/email.html, from() accepts an array (although it's not explicitly stated that you have to use an array).

Try this line instead:

$email->from(array('[email protected]' => 'Admin at gMail'));

Share:
22,190
Johnathan Au
Author by

Johnathan Au

Updated on March 25, 2020

Comments

  • Johnathan Au
    Johnathan Au about 4 years

    I am trying to send an email with CakeEmail and it is failing on $email->to() method. The error messages can be seen on this image: http://i47.tinypic.com/240yq86.png

    Basically, I am getting this two errors:

    Warning (2): preg_match() expects parameter 2 to be string, object given [CORE/Cake/Utility/Validation.php, line 815]
    Warning (4096): Object of class CakeEmail could not be converted to string [CORE/Cake/basics.php, line 566]
    

    This is my sendEmail() method:

    function sendEmail($id, $email, $token)
    {      
        print_r($email);
        $email = new CakeEmail();
        $email->from('[email protected]');
        $email->to($email);
        $email->subject('Activate your account');     
        $activate_url = 'http://' . env('SERVER_NAME') .'/users/activate/'.$id.'/'.$token;
        $message = "Thank you for signing up. Click on the activation link to activate your account \n";
        return $email->send($message.$activate_url);
    }
    
  • Johnathan Au
    Johnathan Au about 11 years
    The error is at the to() method. Nevertheless, I've tried your suggestion and I still get the error.
  • Sepster
    Sepster about 11 years
    @JohnathanAu so what are you passing to to()?? ie do a var_dump ($email); just prior to the to() call. My guess, based on the error message is that it's not a string ;-)
  • Johnathan Au
    Johnathan Au about 11 years
    I am passing in email addresses. I am printing them out as you can see on the first line on the sendEmail method.
  • Sepster
    Sepster about 11 years
    @JohnathanAu :-) I reiterate what I said 10 mins ago... do a var_dump ($email); just prior to the to() call. My guess, based on the error message is that it's not a string ;-)
  • Sepster
    Sepster about 11 years
    @JohnathanAu There's nothing like a fresh set of eyes. I must admit I missed the bleeding obvious problem too... which I do a lot! Which is why I suggested adding some code to confirm/deny our assumptions rather than relying on them!