【发布时间】:2011-01-02 13:55:21
【问题描述】:
我最近posted a question about the CakePHP email component,在一些答案和 cmets 的帮助下,能够解决我的问题...
问题是,解决我的电子邮件问题揭示了另一个问题!
所以,这是我的问题:
我有一份与客户相关联的合同。当我创建合同时,我选择了一个客户。当我将合同保存到数据库时,我想向客户发送一封包含合同链接的电子邮件。但是,当我保存合同时,没有发送电子邮件!我知道电子邮件正在工作。如果我真的对所有变量进行硬编码,那么一切正常。但是,当我尝试从数据库中动态检索客户信息时,它不起作用。
我确实看到了涉及 getByLastId() 的解决方案,但我认为这在这种情况下不起作用,因为我不需要最近添加的用户...我需要附加到的用户这份合同。
我希望我能很好地解释这一点。
这是我认为相关的代码...如果您需要更多信息,请告诉我,我会发布它。
这是我的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));
}
}
任何帮助将不胜感激!
谢谢,
耶利米
【问题讨论】:
-
这乍一看似乎可行。你得到什么输出?来自 Email::smtpError 的任何错误?您在某处有正确的 SMTP 登录信息吗? $this->Contract->User->id 设置是否正确?你的 $User 是什么样的?
-
实际上,我对 PHP 还比较陌生...我如何查看 $User 的样子?我猜我可以使用 debug($User);但我实际上会把它放在哪里?而且,我没有意识到我需要设置 $this->Contract->User->id,因为它是一个参数。就 SMTP 而言,我知道一切正常……因为当我更改 $this->Email->to = "email@example.com" 时,我实际上收到了电子邮件。当我尝试从 $User['User']['email'] 中获取它时,它会停止工作。这就是为什么我认为问题不在于 SMTP 设置。