所以我最近开始了我的第一份真正的工作(耶!),我正在研究一个电子邮件检查器.
它工作得很好,没有错误...只要有电子邮件.
$mbox = imap_open("{.../pop3/novalidate-cert}INBOX","smith@example.com","...");
$inbox = imap_check($mbox);
Run Code Online (Sandbox Code Playgroud)
当收件箱中有电子邮件时,上面的代码工作正常,但如果没有,我会在页面末尾收到此错误:
注意:未知:第0行的"未知"中的邮箱为空(errflg = 1)
没有任何错误抑制似乎能够阻止它被抛出,除了error_reporting(E_ALL^E_NOTICE),我宁愿不使用(一次!)
什么都可以做?
我认为这可能是PHP的差异.
查看此IMAP库https://github.com/barbushin/php-imap.我在一个导入了数千封没有问题的电子邮件的项目中使用了这个.
protected function initImapStream() {
$imapStream = @imap_open($this->imapPath, $this->login, $this->password);
if(!$imapStream) {
throw new ImapMailboxException('Connection error: ' . imap_last_error());
}
return $imapStream;
}
Run Code Online (Sandbox Code Playgroud)
使用了@错误抑制运算符,我猜这是解决方法.
资料来源:https://github.com/barbushin/php-imap/blob/master/src/ImapMailbox.php#L48
编辑:结果您可以通过选项关闭此通知.引自http://php.net/manual/en/function.imap-open.php#73514
你可以避免这个消息:
警告:(null)(); 第0行的"未知"中的邮箱为空(errflg = 1)
通过将选项OP_SILENT指定为imap_open.
我刚刚发现,调用imap_errors()函数可以抑制通知异常。
因此,您唯一需要做的就是将其添加到代码中的某个位置:
$errors = imap_errors();
Run Code Online (Sandbox Code Playgroud)
之后如何处理$ errors变量由您决定。