简体   繁体   English

更多CakePHP电子邮件组件问题

[英]More CakePHP Email Component Troubles

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... 我最近发布了一个关于CakePHP电子邮件组件的问题 ,并且在一些答案和评论的帮助下,能够解决我的问题...

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. 我确实看到了涉及getByLastId()的解决方案,但在这种情况下我认为这种方法不起作用,因为我不需要最近添加的用户……我需要与该合同相关的用户。

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: 这是我的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);
 }

And here is the code in my contracts_controller.php for the add() function: 这是我的contract_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));
   }
  }

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks, 谢谢,

Jeremiah 耶利米

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

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

This method can return multiple values so use pr($User) to see the array format. 此方法可以返回多个值,因此请使用pr($ User)查看数组格式。 You should be able to find the user details in $User[0]['User']['email']. 您应该能够在$ User [0] ['User'] ['email']中找到用户详细信息。

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 我打算将其发布为评论,因为我不确定这是否可行,但是我没有足够的声誉来发表评论:p

Have you tried this? 你有尝试过吗?

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM