tags:

views:

150

answers:

2

I recently posted a question about the CakePHP email component, and, with the help of some of the answers and comments, was able to resolve my problem...

The thing is, solving my email problem revealed another problem!

So, here's my problem:

I have a contract that has a client associated with it. When I create a contract, I select a client. I want to send an email to the client with a link to the contract when I save the contract to the database. However, when I save the contract, no email gets sent! I know that the email is working. If I actually hardcode all of the variables, everything works. But, when I try to dynamically retrieve client info from the database, it doesn't work.

I did see a solution that involved the getByLastId(), but I don't think that will work in this case, because I don't need the most recently added user...I need the user who is attached to this contract.

I hope I am explaining this well.

Here is the code that I think is pertinent...if you need more info, let me know and I'll post it.

Here is the code in my contracts_controller.php for my email sending function:

    function _sendContractEmail($id) {
     $this->Email->smtpOptions = array(
  'port'=>'587',
  'timeout'=>'30',
  'host'=>'smtp.email.com',
  'username'=>'username',
  'password'=>'password'
     );
     $User = $this->Contract->User->read(null,$id);
     $this->Email->delivery = 'smtp';
     $this->Email->to = $User['User']['email'];
     $this->Email->subject = 'New Contract from Company';
     $this->Email->replyTo = '[email protected]';
     $this->Email->from = '[email protected]';
     $this->Email->template = 'default';
     $this->Email->sendAs = 'html';
     $this->set('User', $User);
     $this->Email->send('Test');
     $this->set('smtp-errors', $this->Email->smtpError);
 }

And here is the code in my contracts_controller.php for the 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));
   }
  }

Any help would be greatly appreciated!

Thanks,

Jeremiah

A: 

Use findBy<fieldName>(string $value) ( $this->Contact->User->findById($id) )

http://book.cakephp.org/view/451/findBy

This method can return multiple values so use pr($User) to see the array format. You should be able to find the user details in $User[0]['User']['email'].

HyperCas
A: 

I was going to post this as comment because I'm not sure if this will work, but I don't have enough reputation to make comments yet :p

Have you tried this?

var $uses = array('User');
$user = $this->User->read(null,$id);

or

$user = $this->User->findById($id)
NoOne