How do I use the CakePHP Email Component?

cakephp, email

Solution

http://book.cakephp.org/view/481/Sending-A-Message-Using-SMTP

Use the code sample from the page, specifically the last section to get error notices

/* Check for SMTP errors. */
    $this->set('smtp-errors', $this->Email->smtpError);

You can print the smtp errors using pr(smtp-errors) your view.

The layouts for emails should be located under

/app/view/layout/email/html
/app/view/layout/email/text

If you want copy and modify default templates they can be found under

/libs/view/layouts/email/html
/libs/view/layouts/email/text

Your code looks correct. The problem is probably with the SMTP server rejecting the email.

Oh and you should also check you debug.log and error.log under /app/tmp/logs

Problem

I am new to CakePHP and trying desperately to learn! My most recent struggle is with the Email Component. I have a contract. When I create the contract, I add a user. When I save the new contract...for the first time, I want to send an email to the user in that contract that allows them to click on a link back to the contract, and then accept or reject the contract. How do I send this email? The more details I can get, answer-wise, the better. Everything I have read out there is surprisingly confusing. Do I need to configure smtp settings? How do I grab the user in the contract after it has been saved and pass it and the link to the contract on to the email? How do I know if the email has been sent, without going and checking my email every single time? Here is the code I have in my contracts_controller.php for the email sending function: (A Contract belongsTo a User, and a User hasMany Contracts.) ``` function _sendContractEmail($id) { $this->Email->smtpOptions = array( 'port'=>'465', 'timeout'=>'30', 'host'=>'ssl://smtp.gmail.com', 'username'=>'username', 'password'=>'password' ); $this->Email->delivery = 'smtp'; $User = $this->Contract->User->read(null,$id); $this->Email->to = 'jeremiah@jeremiahotis.com'; $this->Email->subject = ''; $this->Email->replyTo = 'no-reply@goodvaluation.com'; $this->Email->from = 'Jeremiah Oits <jeremiah@jeremiaotis.com>'; $this->Email->template = 'simple_message'; $this->Email->sendAs = 'html'; $this->set('User', $User); $this->Email->_debug = true; $this->Email->send('Test Email'); $this->redirect(array('controller'=>'contracts', 'action'=>'index')); } ``` Here is the code I have in my contracts_controller.php add() function: ``` function add() { if (!empty($this->data)) { $this->Contract->create(); if ($this->Contract->save($this->data)) { $this->Session->setFlash(__('The Contract has been saved', true)); $this->_sendContractEmail($this->Contract->User->id); } else { $this->Session->setFlash(__('The Contract could not be saved. Please, try again.', true)); } ``` And, this is at the top of my contracts_controller.php file after the $name and $helpers: ``` var $components = array('Email'); ``` I guess I should point out that I was having trouble with the template, so to test it, I included the body of the email directly in the send(), and I specified the to email address rather than using a variable. Still...nothing! No error, no email. Any help would be greatly appreciated! I really have no idea what I am doing wrong! Thanks, Jeremiah

Original source