Terrified that her gay best friend would be sent back to the Middle East and victimized after his student visa expired, Liza Monroy went to extreme lengths to keep him in America. Here the 34-year-old writer, whose memoir, “The Marriage Act: The Risk I Took To Keep My Best Friend in America, and What It Taught Us About Love,” will be published Tuesday, tells her extraordinary story of love and law-breaking to The Post’s Jane Ridley.

Emir* and I had been married for more than a year, but it wasn’t until the morning of his green card interview that we finally got around to exchanging rings.

We’d bought them in the Diamond District a week earlier, but Emir had taken them to be engraved, as a special surprise for that day.

My inscription said: “L&Em-Vegas-Frvr.” His, even cheesier, read: “99toEternity-im-Yrs.”

“Cute,” I said, sliding the gold band onto my wedding finger. “Are you nervous?”

According to Section 274 of the United States Immigration and Nationality Act, the maximum punishment for alien smuggling is a fine of $25,000 and 10 years in jail. For the alien, the penalty is immediate deportation. And we were about to take the risk.

We first met in film class at Emerson College in Boston, in 1999. I’m the only child of a single mother and, from the very start of our friendship, Emir became the brother I never had. We had this connection because we both considered ourselves “international students” — outsiders, if you will. He is from the Middle East and, though I’m American, most of my childhood and teenage years were spent abroad because of my mom’s job in the Foreign Service.

Emir was out of the closet to our friends in the US, but not to his family back home. In his country, homosexuals are seen as an abomination, less than human. He told me how, when he was in high school, he’d heard about gays being beaten up and left to die by the highway, with nobody batting an eyelid.

Before he moved to America, his mother had confronted him about, what, in her words was, “a phase.” She begged him to never reveal that side of himself to his father. He was the typical Muslim patriarch and, according to his mom, would have disowned Emir on the spot and even divorce her, too.

“I knew that if I stayed, I would always be one of those married men with children who still go looking for boys online and on street corners, fooling myself and not living my life,” explained Emir.

In Boston, Emir could be himself. And when we spent our last semester in Los Angeles, he was in his element, interning from sun-up to midnight for a studio producer. The trouble was that his student visa was about to expire in December 2001. He’d religiously entered the green card lottery every year since he was a freshman, but he had about a 1 percent chance of being among the 55,000 people randomly chosen as winners.

Then 9/11 happened. Suddenly, it was even harder for Emir to find work because of his Arabic name and the way he looked. “Who will hire me after this?” he said. “If I go back, I’m required to enlist in the mandatory military service. Can you imagine what they’ll do to me in there?”

Over the previous year, I’d told Emir many times that I would marry him if he ever had any visa issues, so he could get a green card. I loved him, not in any sexual sense, but like a member of my family.

But he never took my offers seriously. I don’t think I’d meant them seriously. But now, with the increasing likelihood of him being sent home, I was convinced it was the only solution. So, at a West Hollywood party on Halloween night 2001 — me dressed as a cat and Emir as Harry Potter — I took a gulp of my cocktail, hopped off the bar stool and got down on one knee. “Will you please agree to be my blushing bride already?” I asked.

There was absolutely no way we could tell my mom what we were planning. At the time, she was based out of a consulate in Europe. I didn’t want to put her in the position where she became part of the conspiracy or felt duty-bound to report us.

The wedding, on Nov. 17, 2001, was presided over in Las Vegas by an Elvis impersonator. “Do you promise to walk each other’s hound dogs?” he asked. It was fitting for who we were and what our marriage was about. We shared our first — and only — kiss on the lips as The King jiggled his hips.

After that, Emir and I had to live together for the minimum two years it took for the green card to become permanent.

The challenges we faced as a “couple” were strangely typical, even though the marriage was so different. There were the usual issues about whose turn it was to cook and who did the housekeeping. He’d get irritated because I’d leave a mess. I’d get mad that he often seemed to be out partying with his friends without me.

Five months later, we moved to Manhattan. We settled into a two-bedroom apartment in the East Village and, newly armed with his employment permit, Emir edited movies and I got a position at a talent and literary agency. It was a fun, carefree time because, in New York — the city of immigrants — we felt anonymous.

That said, there were screw-ups, mostly by me. I tried not to tell other people about our “arrangement,” but I got cavalier about it. I was 22 and obviously wanted to date guys. The odd time I really liked someone, after a few dates, I’d drop in the fact that Emir and I were married. It was a litmus test. If they freaked out about it, I didn’t want to be with them.

Emir, meanwhile, was much more cautious about telling anyone. He had a lot more to lose.

The green card interview was in August 2003. Although I was nervous, I didn’t think it would be too much of a big deal. We had bought the wedding bands, our photo album, our documents and all our ducks were in a row. I thought they’d look at our bank accounts and, like in the movie “Green Card,” ask us about the color of our toothbrushes.

In fact, the experience was terrifying. I began to sweat the moment we passed through the metal detectors in the building in Federal Plaza. After an interminably long wait, our names were called.

The agent sat behind his desk, piled high with files and folders. He asked how we met and the date of our wedding. “So soon after 9/11,” he responded.

Then he delivered the bombshell. “Why is there a note in your file regarding a call to our tip line about you two having married solely for the gentleman’s green card?”

My heart started racing. It could have been any number of people who knew Emir and I were married but he was gay. I tried to hide my panic. “Probably an ex-boyfriend with a personal grudge,” I said, thinking on my feet.

“Are you aware that taking shortcuts to get a green card is illegal?” said the agent.

Next, he asked us how often we had sex. “We love sex,” Emir replied. Well-phrased, Emir, I thought to myself. We did love sex, we just didn’t have it with each other. It was all a matter of semantics.

But the agent took Emir into a separate room and, five minutes later, returned alone. He looked me directly in the eye. “Tell me,” he said. “Is your husband circumcised?”

I totally blanked. In our years of friendship and marriage, Emir and I had never seen each other naked. We were very conservative, always changing in our own rooms and dressing at least in shorts and T-shirts in the communal area of the apartment. I’d never walked in on him when he was in the bathroom.

Tears welled in my eyes. “Is that a legitimate question?” I stammered.

When I refused to answer, the agent got up and left the room. That’s it, I thought. Emir is getting deported, I’m about to be arrested and my mother is going to find out.

Then the agent came back in. Emir was with him. To my relief, he wasn’t handcuffed. “We’re going to do an investigation,” the agent announced. “You’ll have to come back for another interview.”

Crushed, we left the INS in silence. It was the middle of the day, but we went to a dark bar and ordered two Cosmopolitans. “I am f - - ked,” said Emir.

But he wasn’t. Just three weeks after our disastrous interview, Emir checked his e-mail. “What? No way! This can’t be real!” he shouted. He had won the green card lottery. That 1-in-1,000 gamble had paid off. We no longer had to prove our marriage was real. He had gotten into America legitimately, so the investigation was scrapped.

It was an incredible relief after going through all the emotions, ranging from desperation to fear. We actually stayed married and lived together in the East Village for another year. It was a safe, comfortable place for us to be.

Inevitably, though, it was time to move on. Towards the end of 2003, my mother agreed to help me buy an apartment in Chelsea. We had to submit paperwork to the co-op board and, to my horror, I remembered that Emir’s name was all over my tax returns. The board needed to know he didn’t have rights to the property. We had to get divorced.

Telling Mom about the sham marriage was the hardest thing I’ve ever had to do. I called her up and, trying to keep my voice calm, came clean about everything. “How could you do this to me?” her voice boomed through the receiver. “It’s not something I did to you,’ I said. “I did it for Emir, and for me.”

Eleven years on, it seems Mom has forgiven me. She retired from the Foreign Service in 2008. I’m happily married and working at a university in California. Emir is now a US citizen, a successful screenwriter and settled in New York City with his longtime partner.

Looking back, I don’t regret what we did for one second. It was an act of true love.

* To protect the identity of the ex-husband, Emir is an assumed name.