In 1987, journeyman actor Jonathan Goldsmith was cast in an episode of the short-lived CBS crime drama “The Law & Harry McGraw” as a Broadway star who winds up dead. To film his funeral, the director rented out a musty, rundown funeral home. As assistants searched for a coffin to use, they were shocked to find, in one, a “woman, elderly, small, and quite dead.”

As he writes in his new memoir, “Stay Interesting: I Don’t Always Tell Stories About My Life, But When I Do, They’re True and Amazing” (Dutton), Goldsmith, best known for playing The Most Interesting Man in the World in a series of popular commercials for Dos Equis beer, asked the assistant director, “Can we please get another coffin?”

“Just get in the f—ing coffin,” the director said as crew members “reached in and yanked the woman out” while Goldsmith got into his creepy position.

“I could smell the toxic traces and stench of death, the formaldehyde, old and stale,” writes Goldsmith, 78. “With the cover closed, I couldn’t help but imagine my own death.”

At first glance, it might seem odd for a man best known for beer commercials to write a memoir. But the book makes clear how well cast Goldsmith was in that role, as his own life has often matched his character’s in terms of pure excitement.

Born in the Bronx in 1938, Goldsmith has spent most of his life trying to make it as an actor. As he learned his trade at the Neighborhood Playhouse in Midtown, Dustin Hoffman was an early nemesis.

“Dustin Hoffman and I never got along,” he writes, noting that as similarly short and “swarthy” Jewish actors, they often competed for roles, and there were personality differences keeping them at odds.

“He was serious, somber, a student of the craft,” Goldsmith writes. “I was more light-hearted . . . more social oriented than Dustin.”

The pair traveled together in a road production called “A Cook for Mr. General,” and after two weeks of irking each other, Goldsmith finally called Hoffman out.

“ ‘I know why you don’t like me,’ I bellowed.” Hoffman “sat there, dumbfounded. ‘Because I’m going to make it and you’re not,’ I stated defiantly, and stood up and left the restaurant.”

“I didn’t return to that lunch,” Goldsmith writes. “But over the next 40 years, I would have those words to eat.”

Hoffman would not be his only celebrity enemy. After he appeared with Clint Eastwood in the 1968 western “Hang ’Em High,” Eastwood never spoke to him again due to a dalliance Goldsmith had with Eastwood’s girlfriend on set.

While Goldsmith never hit the heights of fame, he had great success as a Hollywood stud.

For a time, he frequented a coffee shop called the Pink Turtle inside the Beverly Wilshire Hotel, as it was always filled with “the most beautiful array of starlets.”

In time, he’d find out why — Warren Beatty lived in the hotel’s penthouse, and these women were all hoping to spend time with him. The enterprising Goldsmith used this information to create “an ingenious way to break the ice.”

“ ‘Hi! Are you waiting to see Warren?’ I would inquire. ‘Well, Warren’s tied up at the moment. But he asked me to buy you a drink.’ ”

Throughout the book, Goldsmith documents his often illicit dalliances — paramours included “Jack Warner’s much younger girlfriend, one of Groucho Marx’s wives . . . two congressmen’s wives, and one runner-up to Miss Florida.”

He also discloses that he “broke Henry Fonda’s mistress’ bed.”

Goldsmith describes an intense affair with bombshell Tina Louise, who played Ginger on “Gilligan’s Island,” whom he characterizes as “insatiable.”

“She was the most beautiful woman I had ever been with,” he writes. “She had such great stamina I was afraid I would have a heart attack by the third or fourth round . . . She refused to stop.

“She was a true beauty, tall, elegant, with a cool distance and complete, unfettered surrender.”

Broadway legend Elaine Stritch would wear “a pair of high heels, a mink coat and nothing underneath,” and cook him late-night meals including “lamb chops with Roquefort sauce” at 4 in the morning.

He had one date with Judy Garland and heard horror stories from the set of “The Wizard of Oz,” including how “as a child actress, she would suffer abuse at the hands of studio executives, who passed her around like chattel,” and how “she and the other actors were frequently drugged up.”

While Goldsmith’s own life has been filled with adventure, he says the real “most interesting man” was his good friend and inspiration, the late actor Fernando Lamas.

Lamas’ carefree way of living inspired Goldsmith to seek similar adventures, and later became the basis for the Dos Equis character. (Lamas was also the inspiration for Billy Crystal’s “Fernando” character on “Saturday Night Live.”)

They met when Lamas was directing an episode of the ABC police drama “The Rookies” and Goldsmith auditioned for a role. “I am looking for a crazy mother­f–ker,’ and I think you are perfect,” Lamas told him. Goldsmith was immediately taken with the flamboyant Argentinian.

“Fernando was impeccable,” he writes, “wearing a jacket with an elegant monogrammed silk shirt and loafers. He had a deep tan and luxurious, shining hair. He was the epitome of a movie star.”

The two became great friends, and Lamas regaled him with tales of a life even more fascinating than his own. Lamas had been a middleweight boxing champion in Argentina and would challenge people — including Goldsmith — to fistfights at the slightest provocation, saying things like, “You want to hit me? You want to fight with Fernando? You got the guts?”

Goldsmith had lunch with Lamas and his wife, swimmer/actress Esther Williams, daily, and Lamas would hold court. Lamas had been a longtime regular at the Beverly Hills Hotel, and told of one time when, “after a few guests complained about the shrieking sounds coming from his room, the house detective knocked on the door,” asking if he was OK.

“‘Yes, Butch,’ he replied. ‘It’s nothing. I’ve got Lana in here.’ ” Hollywood legend Lana Turner, that is.

“‘Johnny, she would yell like a banshee, and when she was climaxing, her feet would flap like a bird,’ he told me,” Goldsmith recalls.

While discussing his time with stars like Turner or “La Dolce Vita” star Anita Ekberg, Goldsmith once asked Lamas flat out, “Fernando, how many people did you f–k in Hollywood?”

“After a moment of careful consideration, he stated, matter-of-factly: ‘I think I got most of them.’”

By the time he auditioned for Dos Equis, Goldsmith had fallen on hard times. He was in his late 60s and homeless, sleeping in the back of a pickup truck and showering where he could. He was trying to break back into acting after a 10-year absence and some other failed endeavors. His agent told him about the audition, and he learned they were looking for a young, Latino type. As an elderly Jew, he almost didn’t go, thinking he was wrong for the role.

At the audition, the casting people wanted him to improvise a monologue to end with the sentence, “And that’s how I arm-wrestled Fidel Castro.”

He channeled his friend Lamas, including “mimicking his Argentinian accent and sentence structure.”

He was asked to tell the auditioners about his life, and, Lamas’ example firmly in mind, he regaled them with a tale of how he had been seducing women from a young age. He told how, as a hunter, he happened upon a village of ladies doing laundry by a river, and how he “f–ked them all.” His audition story got wilder from there, ending with Castro challenging him to a duel after he slept with the women in the leader’s life. As exaggerated tales go, it was a howler, and it left the casting people in hysterics.

By the end of the day, Goldsmith was officially The Most Interesting Man in the World.