United Parcel Service will forfeit to the government $40 million that it collected from pharmacies over seven years for delivering drugs that were illegally purchased online without a prescription, federal prosecutors in San Francisco said Friday.

The agreement allows the company to avoid prosecution. In a joint statement, UPS acknowledged it had been on notice that some of the pharmacies were violating the law, but did not close their accounts.

The sales took place across the country from 2003 to 2010. According to the statement by both sides, company officials met with federal law enforcement agencies five times from 2004 through 2006 to discuss UPS role in stopping illegal Internet pharmacies.

A UPS representative testified twice to Congress, in 2004 and 2005, that the Atlanta company was making sure it was not shipping illegally sold medicines or drugs.

At the same time, the joint statement said, a marketing supervisor was discussing sales opportunities from online pharmacies, "both legit and not legit," and UPS shipped packages for one pharmacy for more than a year after authorities ordered it shut down.

Prosecutors said UPS had cooperated with the investigation and had taken steps to guard against future illegal shipments.

"Good corporate citizens like UPS play an important role in halting the flow of illegal drugs," U.S. Attorney Melinda Haag said in a statement.

John Roth, director of criminal investigations at the U.S. Food and Drug Administration, predicted the case would lead to "a significant transformation of illicit Internet pharmacy shipping and distribution practices."

UPS, in a statement, said it has "an obligation and responsibility to help curb the sale and shipment of drugs sold through illegal Internet pharmacies."