codeigniter email->附加错误

时间:2013-05-23 14:35:29

标签: codeigniter email error-handling

我想知道如何在codeigniter实际发送之前捕获电子邮件类错误。

例如,如果函数找不到附件,那么似乎没有错误会在框架内抛出(至少我可以确定)。

$this->email->attach('/path/to/file.ext');

我试过了:

if( !$this->email->attach('/path/to/file.ext') ){ 
  echo $this->email->print_debugger();
}

并且不会抛出任何错误。

我只注意到在发出发送命令后$this->email->send();

附加了附件

事实上,电子邮件已发送,但显然没有附件,但也没有$this->email->message()内容 - 只是一个空的电子邮件正文。

看起来print_debugger()函数在调用send()函数之前确实没有填充。如果我故意指向一个不存在的文件,print_debugger()函数将抛出此错误(仅在调用send()函数之后):

Unable to locate the following email attachment: /path/to/file.ext<br />Your message 
has been successfully sent using the following protocol: sendmail<br /><pre>
User-Agent: CodeIgniter
Date: Thu, 23 May 2013 09:27:49 -0400
<snip>

当我在send()函数之前调用print_debugger()函数时,结果只是一个空数组(我猜,因为该调用的输出只是<pre> </pre>)。

我希望在从框架内调用send()函数之前捕获此错误。

我确实理解我可以原生地使用PHP来检查file_exists,但是对于codeigniter来说,它会比它更早地抛出一些错误,这很好。

if( file_exists('/path/to/file.ext') ):
    $this->email->attach('/path/to/file.ext');
else:
    echo 'ERROR';
endif;

思考/建议/意见?

1 个答案:

答案 0 :(得分:0)

这不是CodeIgniter特有的。我想你正在寻找PHP Exceptions。例如:

try {
    $this->email->attach('/path/to/file.ext');
} catch (Exception $e) {
     echo 'Caught exception: ',  $e->getMessage();
}