Secretary of State Rex Tillerson referred to President Trump as a “moron” after a July 20 meeting at the Pentagon — and was on the verge of resigning amid clashes with the administration, according to a report on Wednesday.

Vice President Mike Pence managed to persuade Tillerson — who is fourth in line to the presidency — to remain in the job at least until the end of the year, officials told NBC News.

Pence has since urged Tillerson to be respectful of the president, urging that any disagreements be sorted out in-house, a White House official told NBC News.

Tensions between Tillerson and Team Trump came to a boil around the time the president delivered a politicized address to the Boy Scouts of America, which Tillerson led for two years, according to the network.

A few days before the speech, Tillerson openly disparaged Trump as a “moron” during a meeting with members of his national security team and cabinet members, three officials told NBC News.

Officials said the administration, which has been buffeted by a series of top-level firings and resignations, would have struggled to cope with the fallout from a cabinet member of Tillerson’s stature quitting so early in the presidency.

But despite the apparent rapprochement, tensions persist between the two men — notably this weekend, when Trump appeared to publicly scold Tillerson over his handling of North Korean crisis.

NBC News interviewed a dozen current and former senior administration officials for its report, as well as others who are close to Trump.

Tillerson — who was in Texas for his son’s wedding when Trump addressed the Boy Scouts — threatened not to return to Washington, three people with direct knowledge of the threats told NBC News.

He stood down after speaking with retired Gen. John Kelly, who would soon replace Reince Priebus as chief of staff, and Defense Secretary James Mattis, four sources told the network.

Pence then gave Tillerson a “pep talk” in Washington, one of these officials said, but also told the secretary that he had to figure out a way to move forward within Trump’s policy framework.

Kelly and Mattis have remained Tillerson’s staunchest allies in the cabinet.

In late July, “they did beg him to stay,” a senior administration official told NBC News. “They just wanted stability.”

The White House declined to comment on the record for the report.