2013-03-25 33 views
2

我試圖發送一封CakeEmail的電子郵件,它在$email->to()方法上失敗。該錯誤消息可以看出這個形象:http://i47.tinypic.com/240yq86.pngpreg_match()期望參數2是字符串,在CakePHP中給出錯誤的對象

基本上,我得到這個兩個錯誤:

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] 

這是我sendEmail()方法:

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); 
} 

回答

3

你有一些基本的PHP錯誤:

變函數參數2名$to;
變更print_r($email)變爲print_r($to);
將$ email->更改爲($ email); to $的電子郵件 - >到($到);`

+0

感謝您的努力,但這真的不是問題=) – 2013-03-25 04:54:20

+2

這就是問題所在。您沒有注意到reciver電子郵件被您的cakeEmail實例取代。所以你得到錯誤 – 2013-03-25 04:57:03

+0

感嘆。現在是凌晨5點。我真的應該阻止這一點。你是對的。我正在克服自己。 – 2013-03-25 04:58:49

1

編輯:徹底無緣明顯問題,其他人已經拿起...

所以,我在尋找其他問題th在有可能打亂類的配置...

http://book.cakephp.org/2.0/en/core-utility-libraries/email.htmlfrom()接受一個數組(雖然它沒有明確地說,你使用數組)。

試試這個行:

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

+0

的錯誤是在以()方法。儘管如此,我已經嘗試了您的建議,但我仍然遇到了錯誤。 – 2013-03-25 04:50:47

+1

@JohnathanAu所以你傳遞給'()'?即在'to()'調用之前執行'var_dump($ email);'。我的猜測,基於錯誤消息是,它不是一個字符串;-) – Sepster 2013-03-25 04:51:48

+0

我通過電子郵件地址。我將它們打印出來,就像您在sendEmail方法的第一行中看到的一樣。 – 2013-03-25 04:53:23

0

請注意,在該PARAM是$email$email = new CakeEmail()變化覆蓋其到一個不同的變量$to

function sendEmail($id, $to, $token) 
{  
    print_r($email); 
    $email = new CakeEmail(); 
    $email->from('[email protected]'); 
    $email->to($to); 
    $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); 
} 
相關問題