Former workers at Yahoo said that the internet company agreed to a US government directive to scan hundreds of millions of email accounts of its customers Justin Sullivan/Getty Images

Yahoo cooperated with the US government to scan hundreds of millions of its customers' email accounts on behalf of the country's spy agencies, it has been reported.

Former Yahoo employees said the internet giant followed orders of the National Security Agency and FBI in secretly constructing custom software which allowed it to search the inboxes of its customers, Reuters reported.

Marissa Mayer
Chief Executive Officer of Yahoo!, Marissa Mayer Reuters

Yahoo's legal team received a classified directive to search Yahoo Mail accounts according to the former employees who said that chief executive Marissa Mayer's decision to obey it led to the departure of its chief information officer Alex Stamos.

Phone and internet companies have handed over data to intelligence agencies before. However, surveillance experts have said that this was the first time a US internet company had agreed to search all arriving messages instead of examining stored messages or scanning a small number of accounts in real time.

Surveillance lawyer Albert Gidari told Reuters: "I've never seen that, a wiretap in real time on a 'selector,'" referring to a search term that would pinpoint specific information.

"It would be really difficult for a provider to do that," he added.

Reuters could not confirm if the demand made in 2015 by the US government went anywhere else, although experts did point out that other internet companies would have likely been approached.

Intelligence agencies can legally ask for customer data from internet companies but this kind of surveillance has been modestly scaled back after the revelations of snooping by former NSA contractor Edward Snowden.

Yahoo has been among companies which have in the past challenged surveillance orders in the Foreign Intelligence Surveillance Court (FISA) but the sources said they did not challenge this one because they thought they would lose.

In a statement to Reuters, Yahoo Inc said: "Yahoo is a law abiding company, and complies with the laws of the United States."

Yahoo's security has been in the headlines after it emerged in September that a hacker got access to 500m customer accounts in 2014.