SHAFAQNA – U.S. President Barack Obama secretly wrote Iran’s Supreme Leader Ayatollah Ali Khamenei in the middle of last month and described a shared interest in fighting Islamic State militants in Iraq and Syria, according to people briefed on the correspondence.

The letter appeared aimed both at buttressing the campaign against Islamic State and nudging Iran’s religious leader closer to a nuclear deal.

Mr. Obama stressed to Mr. Khamenei that any cooperation on Islamic State was largely contingent on Iran reaching a comprehensive agreement with global powers on the future of Tehran’s nuclear program by a Nov. 24 diplomatic deadline, the same people say.

The October letter marked at least the fourth time Mr. Obama has written Iran’s most powerful political and religious leader since taking office in 2009 and pledging to engage with Tehran’s Islamist government.

The correspondence underscores that Mr. Obama views Iran as important—whether in a potentially constructive or negative role—to his emerging military and diplomatic campaign to push Islamic State from the territories it has gained over the past six months.

Mr. Obama and senior administration officials in recent days have placed the chances for a deal with Iran at only 50-50. U.S. Secretary of State John Kerry is set to begin intensive direct negotiations on the nuclear issue with his Iranian counterpart, Javad Zarif, on Sunday in the Persian Gulf country of Oman.

“There’s a sizable portion of the political elite that cut their teeth on anti-Americanism,” Mr. Obama said at a White House press conference on Wednesday about Iran’s leadership, without commenting on his personal overture. “Whether they can manage to say ‘Yes’…is an open question.”

Mr. Obama’s push for a deal faces renewed resistance after Tuesday’s elections gave Republicans control of the Senate and added power both to block an agreement and to impose new sanctions on Iran. Sens. Mark Kirk (R., Ill.) and Robert Menendez (D., N.J.) have introduced legislation to intensify sanctions.

‘There’s a sizable portion of the [Iranian] political elite that cut their teeth on anti-Americanism. Whether they can manage to say ’Yes’…is an open question.’

—Barack Obama

“The best way to prevent Iran from getting a nuclear weapon is to quickly pass the bipartisan Menendez-Kirk legislation—not to give the Iranians more time to build a bomb,” Mr. Kirk said Wednesday.

House Speaker John Boehner (R., Ohio) expressed concern when asked about the letter sent by Mr. Obama.

“I don’t trust the Iranians, I don’t think we need to bring them into this,” Mr. Boehner said. Referring to the continuing nuclear talks between Iran and world powers, Mr. Boehner said he “would hope that the negotiations that are under way are serious negotiations, but I have my doubts.”

In a sign of the sensitivity of the Iran diplomacy, the White House didn’t tell its Middle East allies—including Israel, Saudi Arabia and the United Arab Emirates—about Mr. Obama’s October letter to Mr. Khamenei, according to the people briefed on the correspondence.

Leaders from these countries have voiced growing concern in recent weeks that the U.S. is preparing to significantly soften its demands in the nuclear talks with Tehran. They said they worry the deal could allow Iran to gain the capacity to produce nuclear weapons in the future.

Arab leaders also fear Washington’s emerging rapprochement with Tehran could come at the expense of their security and economic interests across the Middle East. These leaders have accused the U.S. of keeping them in the dark about its diplomatic engagements with Tehran. The Obama administration launched secret talks with Iran in the Omani capital of Muscat in mid-2012, but didn’t notify Washington’s Mideast allies of this covert diplomatic channel until late 2013.

Senior U.S. officials declined to discuss Mr. Obama’s letter to Mr. Khamenei following questions from The Wall Street Journal.

White House press secretary Josh Earnest on Thursday declined to comment on what he called “private correspondence” between the president and world leaders, but acknowledged U.S. officials in the past have discussed the Islamic State campaign with Iranian officials on the sidelines of international nuclear talks. He added the negotiations remain centered on Iran’s nuclear program and reiterated that the U.S. isn’t cooperating militarily with Iran on the Islamic State fight.

Mr. Khamenei has proved a fickle diplomatic interlocutor for Mr. Obama in the past six years.

Mr. Obama sent two letters to Iran’s 75-year-old supreme leader during the first half of 2009, calling for improvements in U.S.-Iran ties, which had been frozen since the 1979 Islamic Revolution in Tehran.

Mr. Khamenei never directly responded to the overtures, according to U.S. officials. And Iran’s security forces cracked down hard that year on nationwide protests that challenged the re-election of then-President Mahmoud Ahmadinejad .U.S.-Iran relations have thawed considerably over the past year, following the election of President Hasan Rouhani. He and Mr. Obama shared a 15-minute phone call in September 2013, and Messrs. Kerry and Zarif have regularly held direct talks on the nuclear

Still, Mr. Khamenei has often cast doubt on the prospects for better relations with Washington. He has criticized the Obama administration’s military campaign against Islamic State, which is also known as ISIS or ISIL, claiming it is another attempt by Washington and the West to weaken the Islamic world.

“America, Zionism, and especially the veteran expert of spreading divisions—the wicked government of Britain—have sharply increased their efforts of creating divisions between the Sunnis and Shiites,” Mr. Khamenei said in a speech last month, according to a copy of it on his website. “They created al Qaeda and [Islamic State] in order to create divisions and to fight against the Islamic Republic, but today, they have turned on them.”

Current and former U.S. officials have said Mr. Obama has focused on communicating with Mr. Khamenei specifically because they believe the cleric will make all the final decisions on Iran’s nuclear program and the fight against Islamic State.

Mr. Rouhani is seen as navigating a difficult balance of gaining Mr. Khamenei’s approval for his foreign policy decisions while trying to satisfy Iranian voters who elected him in the hope of seeing Iran re-engage with the Western world.

The emergence of Islamic State has drastically changed both Washington’s and Tehran’s policies in the Middle East.

Mr. Obama was elected on the pledge of ending Washington’s war in Iraq. But over the past three months, he’s resumed a U.S. air war in the Arab country, focused on weakening Islamic State’s hold of territory in western and northern Iraq.

Iran has had to mobilize its own military resources to fight against Islamic State, according to senior Iranian and U.S. officials.

Tehran’s elite military unit, the Islamic Revolutionary Guard Corps, has sent military advisers into Iraq to help the government of Prime Minister Haider al-Abadi, a close Iranian ally. The IRGC has also worked with Syrian President Bashar al-Assad ’s government, and Shiite militias from across the Mideast, to conduct military operations inside Syria.

U.S. officials have stressed that they are not coordinating with Tehran on the fight against Islamic State.

But the State Department has confirmed that senior U.S. officials have discussed Iraq with Mr. Zarif on the sidelines of nuclear negotiations in Vienna. U.S. diplomats have also passed on messages to Tehran via Mr. Abadi’s government in Baghdad and through the offices of Iraq’s Grand Ayatollah Ali al-Sistani, among the most powerful religious leaders in the Shiite world.

Among the messages conveyed to Tehran, according to U.S. officials, is that U.S. military operations in Iraq and Syria aren’t aimed at weakening Tehran or its allies.

“We’ve passed on messages to the Iranians through the Iraqi government and Sistani saying our objective is against ISIL,” said a senior U.S. official briefed on these communications. “We’re not using this as a platform to reoccupy Iraq or to undermine Iran.”