0

我的操作类中有一个私有函数sendEmail

private function sendEmail($args)
{
    $mailer = sfContext::getInstance()->getMailer();
    $message = $this->getMailer()->compose();
    $address = $this->getFromAddress();
    $message->setFrom(array($address['email'] => $address['fullname']));
    $message->setTo('blah@blah.com');
    $message->setSubject('Subject');
    $message->setBody($this->getPartial('emailTemplate', array('args' => $args)), 'text/html');
    $this->getMailer()->send($message);
}

我从我的Register操作中调用该函数:

public function executeRegister(sfRequest $request)
    {
        sfConfig::set('app_sfForkedApply_from', array(
            'email' => 'blah@blah.com',
            'fullname' => 'fullname'
        ));

        //code for registering

              try {
                    // send email
                    $this->sendEmail($args);
               }
                catch (Exception $e) {
                    $this->logMessage('Email not sent: ' . $e->getMessage(), 'err');
                }

                $out = array('status' => 'success', 'args' => args);
            }            
        }

        $this->getResponse()->setContentType('application/json');
        $this->getResponse()->setContent(json_encode($out));

        return sfView::NONE;
    }

我现在想从同一个动作类中sendEmail的不同动作调用该函数。Register2

public function executeRegister2(sfRequest $request)
    {
        sfConfig::set('app_sfForkedApply_from', array(
            'email' => 'blah@blah.com',
            'fullname' => 'fullname'
        ));

        //code for registering #2

        if ($var = true) {
            try {

                // sending different email
                $this->sendVerificationMail($args);

                $out = array(
                    'status' => 'success',
                    'message' => 'Verification email sent'
                );
            }
            catch (Exception $e) {
                $this->logMessage('Verification email not sent: ' . $e->getMessage(), 'err');

                $out = array(
                    'status' => 'error',
                    'message' => 'Verification email not sent: ' . $e->getMessage()
                );
            }
        }
        else {
            //do other stuff

            $out = array(
                'status' => 'success',
                'message' => 'User activated'
            );
        }

     //added following try-catch to send new email but its not working
        try {
            // send email
            $this->sendEmail($args);

        }
        catch (Exception $e) {
            $this->logMessage('Email not sent: ' . $e->getMessage(), 'err');
        }

        $this->getResponse()->setContentType('application/json');
        $this->getResponse()->setContent(json_encode($out));

        return sfView::NONE;
    }

Register它起作用的动作来看。
Register2操作中,它返回以下错误:

Email not sent: The template "_emailTemplate.json.php" does not exist or is unreadable in "".  

如果我从一个动作而不是另一个动作调用函数,它怎么会找到模板?
它总是将模板转换为 json 文件吗?这可能是问题吗?

4

1 回答 1

1

sfView使用您的请求格式来获取模板的名称。因此,如果您的操作executeRegister2是 Json,它会查找.json.php文件。

您可以在 sendMail 方法中作弊:

private function sendEmail($args)
{
    $mailer = sfContext::getInstance()->getMailer();

    // Added
    $request = sfContext::getInstance()->getRequest();
    $format  = $request->getRequestFormat();
    $request->setRequestFormat('html');


    $message = $this->getMailer()->compose();
    $address = $this->getFromAddress();
    $message->setFrom(array($address['email'] => $address['fullname']));
    $message->setTo('blah@blah.com');
    $message->setSubject('Subject');
    $message->setBody($this->getPartial('emailTemplate', array('args' => $args)), 'text/html');
    $this->getMailer()->send($message);

    // Revert the request format
    $request->setRequestFormat($format);
}

That should do the trick (that being said, sending email while a request can take time, and mess up with your request format: one more reason to make them asynchronous).

于 2013-09-11T10:22:11.467 回答