0

我正在尝试使用 CakePHP (2.3.6) 发送电子邮件邀请,其中用户在输入字段中输入逗号分隔的电子邮件地址列表。

目前,只要没有无效的电子邮件地址,我就可以毫无问题地发送电子邮件。但是,如果有一封错误的电子邮件,我尝试添加一个 Try/Catch 来捕获错误,但我的代码永远不会遇到问题。

这是我所拥有的

try {
            if($Email->send()) {
                $this->Session->setFlash(__('Email successfully sent'), 'flash/success');
            } else {
                $this->Session->setFlash(__('Could not invite guests. Please, try again.'), 'flash/error');
                $this->redirect($this->referer());
            }
        } catch(Exception $e) {
            $this->Session->setFlash(__('Could not invite guests. Probably a bad email. Please, try again.'), 'flash/error');
            $this->redirect($this->referer());
        }
        $this->redirect($this->referer());

当我在调试时输入无效电子邮件时,我收到以下错误:

无效的电子邮件:“foo”错误:发生内部错误。

当调试关闭时:

发生内部错误。

我认为我的 Try / Catch 有问题,但它看起来对我来说是正确的。我应该通过其他方法来捕获 CakePHP 错误吗?

提前致谢!!

4

2 回答 2

0

Check where exactly the exception causing this error is thrown, it is most likely not in the try block you've posted, but in one of the CakeEmail methods (from, sender, replyTo, etc...) that validates the address.

That being said, you should probably either wrap the single CakeEmail method calls into try catch blocks (in case you want to respond with proper error messages depending on which value exactly is wrong), or maybe use a single one that wraps all operations and output a generic error message.

Also note that CakeEmail::send() returns an array (containing header and message data), not a boolean, determining whether an E-Mail was send successfully must be done using try...catch, ie when no exception is being thrown, then the E-Mail was probably sent successfully.

Unfortunately the Cookbook is missing some proper examples. Here's some untested example code that should illustrate how it might work:

$Email = new CakeEmail();

try
{
    $Email->from($from);
    $Email->to($to);
    // etc...

    try
    {
        $Email->send();
        $this->Session->setFlash(__('Email successfully sent'), 'flash/success');
    }
    catch(SocketException $e)
    {
        $this->Session->setFlash(__('Could not invite guests. Please, try again.'), 'flash/error');
    }
}
catch(SocketException $e)
{
    $this->Session->setFlash(__('Bad input'), 'flash/error');
}

$this->redirect($this->referer());
于 2013-09-09T20:03:56.193 回答
0

无效的电子邮件地址会在设置时立即引发异常。这发生在调用 send 方法之前,因此它没有到达您发布的 try 块。

这是来自我编写的一个组件,它在这里为我们的系统处理发送几个不同的电子邮件。我已经将每个地址设置包装在一个 try 块中,以便可以更轻松地追踪有问题的地址,但是如果您不想要那么多细节,您可以将它们全部包装在一个块中。您可以使用 getMessage 方法检查来自异常的消息,以查看有问题的地址字符串。这适用于 Cake 2.3:

$email = new CakeEmail();
//...

// set to for email
try 
{
    $email->to($recipient);
}
catch(SocketException $e) 
{
    $result['problem'] = 'Recipient Address';
    $result['message'] = $e->getMessage();
    return $result;
}

// set cc for email - not required
try 
{
    if($cclist != '') $email->cc(preg_split('/, */', $cclist));
}
catch(SocketException $e) 
{
    $result['problem'] = 'CC List';
    $result['message'] = $e->getMessage();
    return $result;
}
于 2013-09-19T01:51:34.730 回答