A former FBI agent who worked the feds’ investigation into Russian election meddling — and who sent text messages slamming then-candidate Donald Trump — filed a lawsuit Tuesday charging that the bureau caved to “unrelenting pressure” from the president when it fired him.

The suit from Peter Strzok also alleges he was unfairly punished for expressing his political opinions, and that the Justice Department violated his privacy when it shared hundreds of his text messages with reporters.

“The campaign to publicly vilify Special Agent Strzok contributed to the FBI’s ultimate decision to unlawfully terminate him,” said the lawsuit, filed in federal court in Washington, DC, “as well as to frequent incidents of public and online harassment and threats of violence to Strzok and his family that began when the texts were first disclosed to the media and continue to this day.”

The complaint, which names Attorney General William Barr and FBI Director Chris Wray as defendants, revives a controversy that conservative critics of special counsel Robert Mueller’s Russia investigation called proof that the FBI was biased against Trump.

Multiple investigations are underway examining whether the FBI acted properly during the Russia investigation, and Strzok remains a frequent target of Trump’s tweetstorms.

The suit provides new details about Strzok’s firing and defends his reputation months after a congressional hearing in which he asserted that his personal views never influenced his work.

Strzok, a veteran counterintelligence agent who helped lead FBI investigations into Hillary Clinton’s use of a private email server and ties between the Trump campaign and Russia, was removed from Mueller’s team after the texts with his then-lover, FBI lawyer Lisa Page, came to light.

He was fired from the FBI last August.

Many of the texts, on FBI cellphones, were critical of Trump during his 2016 run for office.

They were found by the Justice Department’s inspector general during its investigation of the FBI’s Clinton email probe.

The watchdog office criticized both Strzok and Page but did not find that their work was affected by their personal or political bias.

In the lawsuit, Strzok attorney Aitan Goelman said then-FBI Deputy Director David Bowdich fired him after “unrelenting pressure from President Trump and his political allies in Congress and the media.”

Bowdich overruled the recommendation of a disciplinary official that Strzok merely be demoted and suspended without pay and denied him the chance to appeal.

The lawsuit said the administration discriminated against his viewpoint by firing him even though other government officials who have supported Trump in the workplace have kept their jobs.

In the complaint, Strzok also aims to explain some of the texts that have attracted the most attention, including one in which he told Page “we’ll stop” a Trump presidency.

The text has been interpreted as Strzok saying that he would work to prevent Trump from being elected, but the suit claims the message was meant to reassure Page that the American people would not support a Trump candidacy.

With Post wires