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 e.g.:
$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.