简体   繁体   中英

PHP mail() not sending, but no exception thrown

I'm using CakePHP to send an email. My controller code looks like:

        if ($this->User->save($this->request->data)) {
            $email = new CakeEmail();
            $email->from(array('noreply@mydomain.com' => 'My Domain'));
            $email->to($this->request->data['User']['email']);
            $email->subject('My Domain Confirmation');
            $email->replyTo('noreply@mydomain.com');
            $email->sender('noreply@mydomain.com', 'My Domain');
            $email->emailFormat('html');
            $email->template('confirmation');
            $email->send();
            $email->viewVars(array(
                'name' => $this->request->data['User']['username'],
                'id' => $this->User->getLastInsertID(),
                'code' => $this->request->data['User']['confirm_code']));
        }

I also included at the top of this controller: App::uses('CakeEmail', 'Network/Email');

If I print_r on $email->send(), I get:

Array
(
    [headers] => From: My Domain
Reply-To: noreply@mydomain.com
X-Mailer: CakePHP Email
Date: Thu, 23 Feb 2012 00:40:00 -0800
Message-ID: <4f45fb60a0fc46cd926f305a32396397@mydomain.com>
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
    [message] => 



    Hi there,


Welcome to my site! While you can now vote on submissions and leave comments, your own submissions will be screened and not appear to the public until you click on the confirmation link below:

Click here to confirm your account

We hope to see you around and thanks for joining the community!

So it's obviously using my html email template and passing the right variables to it, and throwing no exceptions. So I decided to just do a basic mail() test within one of my view files eg:

$to = "mytestemail@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

Which echoed "Mail Sent.", but nothing actually came to my mailbox. I checked my file in /var/spool/mail/root and the last email sent was on the same server on Jan. 9, 2012. So it's definitely worked before. I just recently upgraded to Cake 2.0, but this doesn't explain why plain ol' mail() isn't working.

What other debugging methods can I check to make sure it's not my server preventing the email from being sent?

PHP's mail() won't throw any exceptions. You need to check the return status. If that's false, then your MTA isn't accepting mail. Even if it returns true, that doesn't actually mean much of anything .

Take a look at the mail logs in /var/log/. Hopefully those can help you figure out more.

also check your firewall settings. sometimes it stops all outgoing smtp requests if not from specific sources.

The server you are running your Cake app on probably requires (SMTP) authentication before it allows you to send anything, which is a pretty common configuration.

Copy the app/Config/email.php.default file to app/Config/email.php and adjust it to match your setup (usually it's just localhost and you can use one of your mailbox logins for authentication).

Also see the book on this subject.

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