在CakePHP中发送电子邮件afterSave

时间:2011-12-06 00:53:52

标签: email cakephp

我正在尝试创建一个电子邮件组件/模型,它将向数据库添加电子邮件(包括某些字段,包括,来自,主题,消息,创建,修改等)。

数据保存成功后(目前已成功保存),我想实际发送消息。

我认为这对于afterSave()函数来说是最简单的,但我无法收到要发送的电子邮件。

以下是一些相关代码:

电子邮件模型

<?php
class Email extends AppModel {
    var $name = 'Email';

    var $displayField = 'subject';

        function afterSave() {

            $this->Email->to = $this->data['Email']['email'];
            $this->Email->subject = $this->data['Email']['subject'];
            $this->Email->replyTo = $this->data['Email']['email'];
            $this->Email->from = 'Private Message <' . $this->data['Email']['email'] . '>';
            //$this->Email->template = 'simple_message';
            $this->Email->send($this->data['Email']['email_text']);

        }
}

add.ctp for email

<div class="universities form">
<?php echo $this->Form->create('Email');?>
    <fieldset>
        <legend><?php __('Add Email'); ?></legend>
    <?php
        echo $this->Form->input('subject');
        echo $this->Form->input('email_text');
        echo $this->Form->hidden('email', array('value' => $this->params['named']['contact_email']));
        echo $this->Form->hidden('user_from', array('value' => $this->Session->read('User.id')));
        echo $this->Form->hidden('created', array('value' => date("Y-m-d")));
        echo $this->Form->hidden('modified', array('value' => date("Y-m-d")));

    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>

控制器保存代码:

    function add() {
        if (!empty($this->data)) {
            $this->Email->create();
//                        pr($this->data);
//                        die;
            if ($this->Email->save($this->data)) {
                $this->Session->setFlash(__('The email has been saved', true));
            } else {
                $this->Session->setFlash(__('The email could not be saved. Please, try again.', true));
            }
        }
    }

错误我正在尝试发送:

Fatal error: Call to undefined method stdClass::send() in /Users/[USER]/Sites/example_app/app/models/email.php on line 14

新控制器代码:

function add() {
        if (!empty($this->data)) {
            $this->Email->create();
//                        pr($this->data);
//                        die;
            if ($this->Email->save($this->data)) {
                $this->Session->setFlash(__('The email has been saved', true));

                                function _sendMail() {
                                    $this->Email->to = $this->data['Email']['email'];
                                    $this->Email->subject = $this->data['Email']['subject'];
                                    $this->Email->replyTo = $this->data['Email']['email'];
                                    $this->Email->from = 'Private Message <' . $this->data['Email']['email'] . '>';
                                    $this->Email->sendAs = 'text'; //Send as 'html', 'text' or 'both' (default is 'text')
                                    $email->send();
                                }

                                $this->_sendMail();

            } else {
                $this->Session->setFlash(__('The email could not be saved. Please, try again.', true));
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

组件应该用在控制器中,而不是模型中 - 所以最简单的方法是在$ this-&gt; Model-&gt; save()返回true时从控制器发送邮件。

因为您确实将您的模型命名为“电子邮件”,我认为您不能以标准方式使用“电子邮件”组件并需要手动加载:

在控制器中(函数add())

if ($this->Email->save($this->data)) {
    // save was successfull

    App::import('Component', 'Email');
    $email = new EmailComponent();
    $email->startup($this);

    $email->from='joe@example.com';
    $email->to = $this->data['Email']['email'];
    $email->subject = $this->data['Email']['subject'];
    $email->replyTo = $this->data['Email']['email'];
    $email->from = 'Private Message <' . $this->data['Email']['email'] . '>';
    $email->sendAs = 'text'; //Send as 'html', 'text' or 'both' (default is 'text')
    $email->send();


    $this->Session->setFlash(__('The email has been saved', true));
}

然而,可以从模型发送邮件,请参阅此(重复)线程的第二个答案: How do I use the email component from a model in CakePHP?