发送带有SQL的电子邮件会在cakePHP中产生结果

发送带有SQL的电子邮件会在cakePHP中产生结果,php,cakephp,Php,Cakephp,我需要用cakePHP 2向我的所有用户发送每日产品列表 我有以下代码来获取所有用户的电子邮件 $users = $this->User->find('all', array('fields' => array('email'))); foreach ($users as $user) { $this->Email->reset(); $this->Email->from = '<no-reply@test.com.au&g

我需要用cakePHP 2向我的所有用户发送每日产品列表

我有以下代码来获取所有用户的电子邮件

$users = $this->User->find('all', array('fields' => array('email')));
foreach ($users as $user) {
    $this->Email->reset();
    $this->Email->from     = '<no-reply@test.com.au>';
    $this->Email->to       =  $user['email'];
    $this->Email->subject  =  "Daily Products" ;
    $this->Email->sendAs   = 'html';
    $this->Email->send();
}
$users=$this->User->find('all',array('fields'=>array('email'));
foreach($users作为$user){
$this->Email->reset();
$this->Email->from='';
$this->Email->to=$user['Email'];
$this->Email->subject=“日常用品”;
$this->Email->sendAs='html';
$this->Email->send();
}
现在我知道我可以使用一个html模板来处理这个问题,并解析其中的值,但我确实需要在实际视图本身中使用一个foreach循环,并发送产品表

最好的做法是什么?在控制器或视图中编写PHP代码以获取产品


谢谢

这方面的最佳实践是使用shell发送电子邮件。为了避免内存不足,您应该分块阅读用户及其产品,而不是同时阅读所有内容

在foreach循环中,您需要获取每个用户的数据,并将其设置为html模板中可用的任何其他变量,然后您可以在那里呈现所有产品

下面是处理数据的shell中的一些(缩短的)代码:

public function main() {
    $this->loop();
}


public function loop() {
    try {
        while (true) {
            if (!$this->poll()) {
                $this->out(__('Nothing more to process, sleeping...'));
                sleep($this->sleep);
            }
        }
    } catch (Exception $e) {
        $this->out($e->getMessage());
        $this->log($e->getMessage(), 'processing');
    }
}

public function poll() {
    $this->out('Polling...');
    $result = $this->Model->getFirstUnprocessed();

    if ($result === false) {
        return false;
    }

    // do something with the result here

    return true;
}

这应该足以给你一个想法。要成批读取用户,需要在find()选项中增加偏移量。在我的情况下,我只需检查是否有未处理的记录,如果有,我会处理并等待片刻,以便进行下一次尝试。

最佳做法是使用shell发送电子邮件。为了避免内存不足,您应该分块阅读用户及其产品,而不是同时阅读所有内容

在foreach循环中,您需要获取每个用户的数据,并将其设置为html模板中可用的任何其他变量,然后您可以在那里呈现所有产品

下面是处理数据的shell中的一些(缩短的)代码:

public function main() {
    $this->loop();
}


public function loop() {
    try {
        while (true) {
            if (!$this->poll()) {
                $this->out(__('Nothing more to process, sleeping...'));
                sleep($this->sleep);
            }
        }
    } catch (Exception $e) {
        $this->out($e->getMessage());
        $this->log($e->getMessage(), 'processing');
    }
}

public function poll() {
    $this->out('Polling...');
    $result = $this->Model->getFirstUnprocessed();

    if ($result === false) {
        return false;
    }

    // do something with the result here

    return true;
}
这应该足以给你一个想法。要成批读取用户,需要在find()选项中增加偏移量。在我的例子中,我只需检查是否有未处理的记录,如果有,我会处理并等待片刻,以便进行下一次尝试。

电子邮件的“视图”实际上是一个元素。在视图/元素/电子邮件下。有两个文件夹,分别是
html
text
,它们都用来保存各自的模板

您可以在那里进行foreach,然后确保使用以下设置控制器中的布局:

$this->Email->sendAs = 'html'; // Can also be 'text' or 'both' (for multipart).
$this->Email->layout = 'foo'; // Would include Views/Elements/email/html/foo.ctp
虽然电子邮件组件自CakePHP 2.0以来就已被弃用,但您应该改用CakeEmail组件。有关如何使用它的更多详细信息,请参阅。

电子邮件的“视图”实际上是一个元素。在视图/元素/电子邮件下。有两个文件夹,分别是
html
text
,它们都用来保存各自的模板

您可以在那里进行foreach,然后确保使用以下设置控制器中的布局:

$this->Email->sendAs = 'html'; // Can also be 'text' or 'both' (for multipart).
$this->Email->layout = 'foo'; // Would include Views/Elements/email/html/foo.ctp

虽然电子邮件组件自CakePHP 2.0以来就已被弃用,但您应该改用CakeEmail组件。有关如何使用它的更多详细信息,请参阅。

谢谢您的回复。我知道你的代码可以解决foreach内存问题,但我还是有点不知道如何从电子邮件本身的查询中获得结果。谢谢你的回复。我知道你的代码可以解决foreach内存问题,但我仍然不知道如何从电子邮件本身的查询中获得结果。