Iran's former hard-line president Mahmoud Ahmadinejad has sent a 3,500-word letter to US president Donald Trump that applauds immigration and says it shows "the contemporary US belongs to all nations".

The missive strikes a somewhat conciliatory tone and is not the first dispatch sent by Mr Ahmadinejad, who has counted US presidents George W Bush and Barack Obama among his pen pals.

But this letter comes as criticism of Mr Trump over his travel ban affecting seven Muslim-majority countries including Iran mounts in Tehran.

It also may serve to burnish Mr Ahmadinejad's image domestically after the nation's Supreme Leader warned him not to run in Iran's upcoming May presidential election.

In the letter, published by Iranian media outlets, he noted Mr Trump won the election while he "truthfully described the US political system and electoral structure as corrupt".

Mr Ahmadinejad decried US "dominance" over the United Nations, as well as American meddling in the world that has brought "insecurity, war, division, killing and displacement of nations".

He also acknowledged the immigration of some one million Iranians to America, saying that US policies should "value respect toward the diversity of nations and races".

"In other words, the contemporary US belongs to all nations, including the natives of the land," he wrote. "No one may consider themselves the owner and view others as guests or immigrants."

A judge later blocked Mr Trump's travel ban and an appeals court refused to reinstate it. He has promised to issue a revised order soon, saying it is necessary to keep America safe.

Entirely missing from the letter was any reference to Iran's nuclear programme.

Under Mr Ahmadinejad's presidency, Iran found itself heavily sanctioned over the programme as Western governments feared it could lead to the Islamic Republic building atomic weapons.

Iran has long maintained its programme was for peaceful purposes.

Iran under current president Hassan Rouhani struck a nuclear deal with world powers, including the Obama administration, to limit its enrichment of uranium in exchange for the lifting of some sanctions.

Mr Trump campaigned promising to renegotiate the deal, without offering specifics.

Ahmadinejad gave the letter to the Swiss Embassy in Tehran, which represents US interests in Iran.

It comes ahead of Iran's presidential election, in which Mr Rouhani is widely expected to seek a second four-year term.

While allies of Mr Ahmadinejad are expected to run, he himself will not after Supreme Leader Ayatollah Ali Khamenei warned him in September his candidacy would bring about a "polarised situation" that would be "harmful for the country".

Mr Ahmadinejad's popularity in Iran remains in question. During his tenure, he personally questioned the scale of the Holocaust and predicted the demise of Israel.

His disputed 2009 re-election saw widespread protests and violence. Two of his former vice presidents went to prison for corruption.

But Mr Ahmadinejad offered Mr Trump his own warning about how quickly time passes for leaders.

"Four years is a long period, but it ends quickly," he wrote. "The opportunity needs to be valued, and all its moments need to be used in the best way."