我最近在posted a question about the CakePHP email component上,在一些答案和评论的帮助下,解决了我的问题……
问题是,解决我的电子邮件问题揭示了另一个问题!
所以,我的问题是:
我有一个合同,其中有一个客户与之关联。当我创建合同时,我选择了一个客户。当我将合同保存到数据库时,我想向客户端发送一封电子邮件,其中包含合同的链接。但是,当我保存合同时,不会发送电子邮件!我知道电子邮件是有效的。如果我真的硬编码了所有的变量,一切都会正常工作。但是,当我尝试从数据库中动态检索客户端信息时,它不起作用。
我确实看到了一个涉及getByLastId()的解决方案,但我认为在这种情况下不会起作用,因为我不需要最近添加的用户……我需要附加到此合同的用户。
我希望我解释得很好。
这是代码,我认为是pertinent...if你需要更多的信息,让我知道,我会张贴它。
以下是我的contracts_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));
}
}任何帮助都将不胜感激!
谢谢,
耶利米
发布于 2010-01-08 23:34:28
使用findBy<fieldName>(string $value) ($this->联系人->用户->findById($id))
http://book.cakephp.org/view/451/findBy
此方法可以返回多个值,因此使用pr($User)查看数组格式。您应该能够在$ user‘’email‘中找到用户详细信息。
发布于 2010-01-09 00:13:19
我打算以评论的形式发布这篇文章,因为我不确定这是否会起作用,但我还没有足够的声誉来发表评论:
你试过这个吗?
var $uses = array('User');
$user = $this->User->read(null,$id);或
$user = $this->User->findById($id)https://stackoverflow.com/questions/2021983
复制相似问题