BERLIN — Spain's Telefonica has agreed to buy German mobile unit E-Plus from Dutch telecommunications company Royal KPN NV for 5 billion euros ($6.58 billion) plus shares, a deal that would create Germany's biggest cellphone operator in terms of customers.

The proposed deal announced Tuesday would bring together E-Plus, currently Germany's No. 3 operator with 23.4 million customers, with O2 — the fourth-biggest, which is already owned by Telefonica and has 19.3 million customers.

Telefonica's German unit, Telefonica Deutschland, said it expects savings of between 5 billion and 5.5 billion euros, and will benefit from combining the two companies' distribution and customer service functions and networks.

The plan foresees KPN getting a total of 5 billion euros in cash and a 17.6 percent stake in Telefonica Deutschland, the Dutch company said, adding that the transaction values E-Plus at 8.1 billion euros.

The deal is subject to shareholder and antitrust approval. The companies expect the transaction to close in mid-2014.

Germany's biggest mobile operator at present is Deutsche Telekom AG's T-Mobile, which serves some 36.6 million people; Vodafone is second with 33.9 million customers. The combination of E-Plus and O2 would have nearly 43 million; Telefonica put their combined revenues last year at 8.6 billion euros.

"This transaction is a strong commitment to the German market and a significant step in Telefonica's growth story — providing us with a leading position within the strongest economy in Europe," Eva Castillo, the chairwoman of Telefonica Deutschland's supervisory board and the chief executive of Telefonica Europe.

The German economy is Europe's biggest and has continued to grow even as countries across large parts of Europe struggle with public debt and recessions.

Telefonica has mobile and fixed-line operations across Europe and Latin America. The company said the German deal would make it Europe's second-biggest operator in terms of cellphone customers.

Telefonica, which has been working to reduce its debt, noted that "this transaction will mainly be financed via financial instruments, which will enable to maintain Telefonica's leverage ratio stable."

The announcement came as KPN said its second-quarter net profit fell to 108 million euros, down 68 percent from the same period a year ago due to tough market conditions for its mobile phone operations and an ongoing decline in fixed-line services.

KPN said its revenues in the second quarter dropped 8 percent to 2.9 billion euros from 3.2 billion euros a year earlier. CEO Eelco Blok said the company will now "focus on its core geographies," and the company said it will use most of the cash from the sale to "increase its financial flexibility."

KPN shares were up 5.3 percent compared with Monday's close at 1.90 euros. The deal also sent Telefonica's shares 2.4 percent higher to 10.31 euros in early trading in Madrid.

In Germany, rival Deutsche Telekom — which will face one fewer competitor if the deal goes through — was one of the biggest winners on Frankfurt's DAX index of blue chip stocks. Its shares climbed 1.8 percent to 9.35 euros.