A California woman hatched a plot that could have sent her husband’s former fiancée to prison for life — but her efforts backfired, and now she’s the one behind bars, authorities say.

The scheme seems ripped from a Hollywood potboiler: Angela Maria Diaz, 31, the jealous spouse, wrote threatening e-mails to herself, posted a Craigslist ad saying she wanted to fulfill a “rape fantasy” and even faked injuries from the supposed attack — and then pinned it all on Michelle Suzanne Hadley, 30.

Hadley was arrested last year and spent three months in jail — but officials Monday announced that she’d been cleared and that Diaz was now facing charges for concocting the whole thing.

“When a person who has committed a crime gets arrested and charged, that’s a bad day! But when someone who is innocent gets arrested and charged with some crime, that’s not just a bad day – it’s a nightmare!” Orange County District Attorney Tony Rackauckas said in a statement. “This is the tale of a woman who faced such a nightmare, but with the help of her attorney and law enforcement, who continued to seek the truth, she will be able to be exonerated.”

The sordid tale began in August 2013, when Hadley started dating a man identified in court papers as “John Doe,” a 39-year-old deputy US marshal. The two bought a condo and were set to get married, but he called it off two years later.

The man then met Diaz on a dating site in early 2016, and they moved in together and were married two months later. But things turned dark when Diaz lied to her husband that she was pregnant — and allegedly launched her plan against Hadley.

The prosecutor said Diaz’s motive was unclear but speculated there was a “love triangle” and that the suspect may have wanted to “put the dagger into the older relationship,” according to the Los Angeles Times.

Regardless, the plot was as unhinged as it was precise — and it worked for awhile.

Diaz claimed to cops in June that Hadley sent her e-mails that threatened that she would be raped and killed along with her fictitious unborn child, Rackauckas said. The hundreds of bogus e-mails included pictures of beheaded corpses, aborted fetuses and dead women. Later, Diaz also told cops that Hadley had impersonated her on Craigslist and posted an ad requesting a “rape fantasy” scenario.

Everything came to a head when Diaz called 911 to report that a man tried to rape her in her garage. She said the man bashed her head against a wall and tore her shirt. It was all a hoax.

But she blamed Hadley for everything, and police believed her story. Hadley was arrested on felony charges. She posted $100,000 bail and was released.

Several weeks later, however, Diaz summoned police to her home where a 17-year-old boy had actually shown up in response to the rape-fantasy ad.

“Due to the seriousness of the threats, the alleged attack on Diaz in her garage, the escalation in both the frequency and nature of the threats, and yet another man arriving at Diaz’s residence to engage in a rape fantasy encounter, law enforcement believed a serious threat to public safety existed’, according to the press release issued by Rackauckas.

This time, Hadley was charged with stalking, criminal threats, attempted rape, assault with intent to commit rape during a residential burglary, violating a protective order and a crime-bail-crime enhancement.

She was held on $1 million bail.

But Hadley’s lawyer, Michael Guisti, was eventually able to convince authorities it was all a con job.

Diaz was shown to have doctored some of the threatening e-mails to make them seem like they came from Hadley’s account. Another giveaway: Diaz used some identical language that Hadley had written to her ex-fiancee. It also was proven that Diaz was the one who actually posted the Craigslist ad and told men who responded that she wanted to be raped even if she screamed.

She attached photos of herself to the ad and described her daily routine, Rackauckas said. And then she faked injuries to her head and neck to make it look like she was attacked.

Hadley was finally freed from jail in October, after three months behind bars.

Diaz was charged with numerous felony charges including two counts of kidnapping, two counts of false imprisonment, perjury, two counts of forgery, two counts of possession of forged instrument, grand theft, one misdemeanor count of filing a false report with an agency, and 21 misdemeanor counts of filing a false police report.

She faces more than 20 years behind bars.