Their aim was to spread fear, to destroy huge swaths of the United States’ largest city, to turn the tide of a war, to influence a presidential election.

Yet it wasn’t a modern plot by al Qaeda — it was a Manhattan terrorist attack planned by Confederate officers in 1864, a nearly devastating (and almost forgotten) chapter of New York City history.

The vast and fiendish plot originated in Toronto, as an attempt by the Confederate Secret Service to disrupt the Nov. 8 re-election of President Abraham Lincoln. With the battles of the Civil War going against the South, Confederate officials hoped that Lincoln could be defeated by a peace-seeking Democrat — if the electorate saw how easily the Confederacy could strike at the North’s largest cities.

The original targets included Chicago, Boston, Cincinnati and New York. Ultimately, the Emerald City, as it was then known to its 814,000 residents, was one of cities chosen. Fires would be set throughout Manhattan, destroying homes, businesses and hope.

The attack was to be carried out by six officers, all in their 20s, with the oldest being 29. They sincerely believed that 20,000 of New York’s residents would rally to their cause. They had been told by a handful of well-placed New Yorkers that once the fires started, an army of New York civilians would rush down to City Hall to raise a flag that most Manhattanites would not recognize — the Second National Flag of the Confederacy, a white banner with the familiar Confederate battle flag in the upper left.

But one double agent working for the Union, and another still unknown source in Canada, alerted the Lincoln administration to the plot. Telegrams were dashed off to mayors. On Nov. 3, 1864, The New York Times reprinted the telegram with the headline: “REBEL CONSPIRACY TO BURN NORTHERN CITIES.” The body of the article explained that rebels intended to set “fire to the principal cities in the Northern States on the day of the Presidential election.”

Curiously, though, the Times article explained that the mayor of Buffalo had received the telegram, but no mention was made that New York City’s Mayor C. Godfrey Gunther had received the same threat. The Confederates never targeted Buffalo, but New York City had been mentioned as a potential firebombing target in a Richmond Whig editorial on Oct. 15, an editorial which the Times reprinted without any elaboration on Oct. 18. Most New York City residents probably thought they were safe.

Nonetheless, acting on tips, the Lincoln administration sent 3,500 Union troops to guard New York City polls. That spooked the Confederates’ contacts, including James McMaster, editor of the Freeman’s Journal and Catholic Digest.

McMaster, imprisoned early in the war for his anti-Lincoln editorials, had promised the Confederates who arrived in the city on Oct. 27 that an army of “Copperheads” would take over the city once the fires halted polling. Yet when the Union troops arrived, McMaster told the Confederates that they could expect no support at all in carrying out the plot.

Election Day passed without incident, and Lincoln was re-elected. The Times ran a remarkable editorial on Nov. 16 thanking the just-departed Union forces for keeping peace, and then challenged New Yorkers whom it deemed as Southerners or sympathizers with being “too slothful or cowardly to brave the dangers and fatigue of the battlefield.”

The Confederates, who had waited out the Union troops, may have reacted to the bluster.

They struck on Nov. 25, the Friday after the nation’s second official Thanksgiving, setting fires in the rooms they occupied in 21 commercial hotels, mostly along Broadway. Their weapon was “Greek fire,” a complex chemical compound that spontaneously combusts when exposed to air. They had been given 144 vials of the material by an unnamed chemist living just west of Washington Square.

None of the room fires caught beyond burning some bed linen. The Confederates, who had not practiced with the compound, had left their hotel room windows closed. That robbed the Greek fire of the oxygen it needed to spread. Police and firemen rushed to the hotels and extinguished the flames.

The Confederates picked the wrong targets. On the city’s west side was the Manhattan Gas Works, where coal was distilled into gas that lit the city’s homes and businesses. Had the Gas Works’ pressure-regulating tanks been sabotaged, gas flowing into the city’s homes and businesses would have made any lit match a potential firebomb.

If the Confederates had been better saboteurs, spies and scouts, there is little doubt New York City would have burned down in 1864 — perhaps changing the course of the Civil War.

Clint Johnson will give a PowerPoint presentation on his book: “A Vast and Fiendish Plot: The Confederate Attack on New York City” (Citadel Press) at the the NY Public Library, 455 Fifth Ave., tomorrow at 6:30 p.m. Admission is free.