Hillary Clinton solicited a $12 million donation from a government that her State Department considered corrupt, then realized the “mess” it would cause in her presidential run, a newly leaked ­email reveals.

King Mohammed VI of Morocco agreed to give the money to the Clinton Foundation, provided that it held a convention in his country in May 2015 with Clinton as the keynote speaker.

But Clinton realized that the conference, slated for a month after she announced her run for president, would hurt her candidacy.

“No matter what happens, she will be in Morocco hosting CGI [Clinton Global Initiative] on May 5-7, 2015. Her presence was a condition for the Moroccans to proceed so there is no going back on this,” top Clinton aide Huma Abedin wrote to campaign manager Robby Mook in a November 2014 email revealed by Wiki­Leaks.

In another email, Abedin warned that if Clinton didn’t attend, the $12 million would be off the table.

“Just to give you some context, the condition upon which the Moroccans agreed to host the meeting was her participation. If hrc was not part if it, meeting was a non-starter,” Abedin wrote in a January 2015 ­email to Mook and campaign manager John ­Podesta.

“CGI also wasn’t pushing for a meeting in Morocco and it wasn’t their first choice. This was HRC’s idea, our office approached the Moroccans and they 100 percent believe they are doing this at her request. The King has personally committed approx $12 million both for the [foundation’s] endowment and to support the meeting,” Abedin continued.

“It will break a lot of china to back out now when we had so many opportunities to do it in the past few months. She created this mess and she knows it.”

The king gave the money to the Clinton Foundation and underwrote the CGI summit with the quid-pro-quo understanding that Hillary would attend, and other dignitaries attending were led to believe that she would be there.

But Hillary sent Bill and Chelsea Clinton while she campaigned in Nevada and California.

The deal with Morocco was struck even though the State Department — under Hillary Clinton — accused the country’s government of “arbitrary arrests and corruption,” according to Fox News.

It was unclear exactly how much went directly for the summit and how much went to the Clinton Foundation — but the total added up to $12 million, according to the emails.

Guests at the May 2015 CGI summit — which was closed to the media — stayed at the five-star Selman-Marrakesh hotel, a posh lodging featuring a stable of Arabian horses and luxury amenities.

The meeting was officially paid for by OCP, a Moroccan government-owned mining company that has been accused of human rights violations.

Hillary Clinton supported the monarch when she was secretary of state, and the US-financed Export-Import Bank gave OCP a $92 million loan guarantee during her tenure, the Daily Caller reported.

Politico reported in 2015 that Clinton “was seen by [the Moroccan capital] Rabat as among its staunchest supporters in the Obama administration.”

The Robert F. Kennedy Center for Justice charged OCP with “serious human-rights violations,” the Daily Caller said.

And Mohamed Yeslem Beisat, the Washington envoy for the self-described “government in exile” Polisario Front, told Al-Monitor, a news site that focuses on the Middle East, in 2013 that “OCP is the first beneficiary of the war and the first beneficiary of the occupation — it is the one that is cashing in on the misery of thousands of refugees and hundreds of political detainees for the past 40 years.”

Beisat added, “They’re doing this because they know Hillary has some chances of being president of the United States. And they want her to support their brutal occupation of Western Sahara.”

Human rights advocates have called the company’s mined phosphates “blood phosphates” — similar to the term “blood diamonds,” referring to precious stones mined in war zones and/or used to finance deadly conflicts.

Abedin’s email was among more than 4,000 posted Thursday by WikiLeaks.

They were stolen from Podesta, who has warned that the communications may have been doctored.

US intelligence agencies have fingered the Russian government as the culprits behind the hack — charging that Russian President Vladimir Putin was trying to influence the US election in Donald Trump’s favor.

In Wednesday’s final presidential debate, Trump insisted — without offering evidence — that “no one knows” who hacked the ­emails.

Clinton slammed her opponent for using the purloined emails to attack her, as well as for his past statements encouraging the Russians to hack her emails from when she was secretary of state.