更多CakePHP电子邮件组件故障

时间:2010-01-07 16:49:34

标签: email cakephp

我最近posted a question about the CakePHP email component,并且在一些答案和评论的帮助下,能够解决我的问题......

问题是,解决我的电子邮件问题揭示了另一个问题!

所以,这是我的问题:

我有一个与之关联的客户合同。当我创建合同时,我选择了一个客户。当我将合同保存到数据库时,我想向客户发送一封电子邮件,其中包含合同的链接。但是,当我保存合同时,没有发送电子邮件!我知道电子邮件正在运行。如果我实际上硬编码所有变量,一切都有效。但是,当我尝试从数据库中动态检索客户端信息时,它不起作用。

我确实看到了一个涉及getByLastId()的解决方案,但我不认为在这种情况下会起作用,因为我不需要最近添加的用户......我需要附加到的用户这份合同。

我希望我能解释清楚。

以下是我认为相关的代码...如果您需要更多信息,请告诉我,我会发布。

以下是我的contract_controller.php中的代码,用于我的电子邮件发送功能:

    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 = 'noreply@example.com';
     $this->Email->from = 'noreply@example.com';
     $this->Email->template = 'default';
     $this->Email->sendAs = 'html';
     $this->set('User', $User);
     $this->Email->send('Test');
     $this->set('smtp-errors', $this->Email->smtpError);
 }

这是我的contracts_controller.php中add()函数的代码:

    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));
   }
  }

非常感谢任何帮助!

谢谢,

2 个答案:

答案 0 :(得分:0)

使用findBy<fieldName>(string $value)($ this-&gt;联系人 - &gt;用户 - &gt; findById($ id))

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

此方法可以返回多个值,因此请使用pr($ User)查看数组格式。您应该能够在$ User [0] ['User'] ['email']中找到用户详细信息。

答案 1 :(得分:0)

我打算将此作为评论发布,因为我不确定这是否有效,但我还没有足够的声誉来发表评论:p

你试过这个吗?

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

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