简体   繁体   中英

Cakephp email sent but not received

I am running Cakephp 2.4.3 and am trying to send an email with CakeEmail.

I have this code at the top of my UsersController:

App::uses('AppController','Controller','CakeEmail','Network/Email');

this function in the Controller:

function send_email(){
    try{
        $email = new CakeEmail('test');
        $email->from('sender@example.com');
        $email->to('me@gmail.com');
        $email->subject('Test');
        if($email->send('Test Message'))
            echo 'Success';
        else echo 'Failure without Exception';
    } catch (Exception $e){
        echo 'Failure with Exception';
    }
}

and this in Config/email.php

public $test = array(
  'log' => true
);

When I go to the url: mysite.com/users/send_email

it says "Success" and the debug log says:

2014-05-30 22:49:43 Debug: 
From: sender@example.com
X-Mailer: CakePHP Email
Date: Fri, 30 May 2014 22:49:43 +0000
Message-ID: <53890b07e3b44b2b88132a62ad2deb05@mysite.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Test Message

but I never get an email coming into my Gmail account. I have checked my spam folder as well. What could be the issue and how else can I debug this?

UPDATE:

My cake app is being hosted on the web. I tried using a different email address at a different domain and changing the email code to this:

try{
    if(mail('xxx@iioengine.com','Test Subject','Test Message'))
        echo 'Success';
    else echo 'Failure without Exception';
} catch (Exception $e){
    echo 'Failure with Exception';
}

but the result did not change.

Sending mail generally doesn't work from localhost. You can try and set the port in the settings, but your isp will probably have the port blocked.

Just checking the specs: http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

It looks like maybe your new declaration shouldn't have test inside:

$Email = new CakeEmail();

To debug, you could try sending an email to a different host (not gmail). You could also try using the default php mail() function, which is actually quite simple to use and very similar to cake's layout, not surprisingly.

http://php.net/manual/en/function.mail.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM