2
votes

I have a Component that I use to send off emails in CakePHP 2.0. It was working perfectly up until I put the site on a live host. Now, I get the error:

Invalid email: "[email protected]"

Obviously, the above email address is valid, so I don't know why Cake is throwing an Exception here. Note that I get the same result, regardless of what email address I use.

Component function that I use:

public function send($to, $subject, $body = null, $view = 'default', $vars = array(), $layout = 'layout'){
    try{
        $Email = new CakeEmail();
        $Email->config('default'); 
        $Email->template($view, $layout)->viewVars($vars);
        $Email->from(array(Configure::read('Site.email') => Configure::read('Site.name')));
        $Email->to($to);
        $Email->subject($subject);

        if(strlen(trim($body)) > 0){
            $Email->send($body);
        } else{
            $Email->send();
        }
    } catch(Exception $e){
        var_dump($e);
    }

}

In my email config file, I'm using the default Mail transport.

Update: I seem to be having the exact same issue as the person in this question. Basically, I'm getting the error:

Invalid email: "[email protected]" An Internal Error has occured.

2
[email protected] is the email stored in $to or Configure::read('Site.email') variable?Arun Jain
@ArunJain [email protected] is stored in Configure::read('Site.email'); I've manually added different email addresses, to no avail. I even commented out the CakeEmail::from call, only to be given an exception for the CakeEmail::to call, which was [email protected]Wayne Whitty
If the problem trickles to the to line when the from is commented out, then I would suspect it happens earlier. Comment out the template line and see if the code works error free.AgRizzo
@AgRizzo I commented out the template and also the config call, just to see what would happen. Still getting an invalid error on the from email. Starting to think this could be host-related.Wayne Whitty
Sounds like it's most probably a value that you've set in your email configuration. Have you checked the stack trace to figure out from where this actually stems? Are you using a custom email pattern? Have you checked whether the internally used filter_var($email, FILTER_VALIDATE_EMAIL) successfully validates the address? ... ps, please always mention your exact CakePHP version!ndm

2 Answers

2
votes

OK, I feel a bit silly now:

Basically, I realized that I didn't set the content type in my email config. On my local machine, I'm using Google's email server to send HTML emails. On the production server, I was using the regular Mail configuration. Unfortunately, I forgot to set the content type for this particular type of mail (whereas it was set for the Gmail config). The issue here is that many of my layout & view files for HTML emails didn't exist for textual-based emails. This seems to have broken Cake in some form or another, leading it to dump out nonsensical errors about emails being invalid, even though they were.

To be honest, I stumbled upon this by accident. Yesterday, I uploaded some code to try and debug the issue. To my surprise, my test code worked. It then dawned on me that in my test code, I was manually specifying default views and layouts. i.e. I wasn't using any of my custom views.

To test my theory out, I made sure that the HTML views and layouts also existed for textual-based emails. Sure enough, my emails began to send.

Since then, I've set the content type to HTML and everything is working as expected.

0
votes

Some ideas

$Email->to(Sanitize::paranoid($to, array('@'));

Also, make sure the ->to() function is actually setting $to to ->to :)

Otherwise, set it up like this:

$Email->to = Sanitize::paranoid($to, array('@');